LCOV - code coverage report
Current view: top level - gcc - gimple-fold.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 92.5 % 5693 5267
Test Date: 2026-12-05 16:10:02 Functions: 96.8 % 155 150
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 "case-cfn-macros.h"
      73              : #include "gimple-range.h"
      74              : 
      75              : enum strlen_range_kind {
      76              :   /* Compute the exact constant string length.  */
      77              :   SRK_STRLEN,
      78              :   /* Compute the maximum constant string length.  */
      79              :   SRK_STRLENMAX,
      80              :   /* Compute a range of string lengths bounded by object sizes.  When
      81              :      the length of a string cannot be determined, consider as the upper
      82              :      bound the size of the enclosing object the string may be a member
      83              :      or element of.  Also determine the size of the largest character
      84              :      array the string may refer to.  */
      85              :   SRK_LENRANGE,
      86              :   /* Determine the integer value of the argument (not string length).  */
      87              :   SRK_INT_VALUE
      88              : };
      89              : 
      90              : static bool
      91              : get_range_strlen (tree, bitmap, strlen_range_kind, c_strlen_data *, unsigned);
      92              : 
      93              : /* Return true when DECL can be referenced from current unit.
      94              :    FROM_DECL (if non-null) specify constructor of variable DECL was taken from.
      95              :    We can get declarations that are not possible to reference for various
      96              :    reasons:
      97              : 
      98              :      1) When analyzing C++ virtual tables.
      99              :         C++ virtual tables do have known constructors even
     100              :         when they are keyed to other compilation unit.
     101              :         Those tables can contain pointers to methods and vars
     102              :         in other units.  Those methods have both STATIC and EXTERNAL
     103              :         set.
     104              :      2) In WHOPR mode devirtualization might lead to reference
     105              :         to method that was partitioned elsewhere.
     106              :         In this case we have static VAR_DECL or FUNCTION_DECL
     107              :         that has no corresponding callgraph/varpool node
     108              :         declaring the body.
     109              :      3) COMDAT functions referred by external vtables that
     110              :         we devirtualize only during final compilation stage.
     111              :         At this time we already decided that we will not output
     112              :         the function body and thus we can't reference the symbol
     113              :         directly.  */
     114              : 
     115              : static bool
     116      4469067 : can_refer_decl_in_current_unit_p (tree decl, tree from_decl)
     117              : {
     118      4469067 :   varpool_node *vnode;
     119      4469067 :   struct cgraph_node *node;
     120      4469067 :   symtab_node *snode;
     121              : 
     122      4469067 :   if (DECL_ABSTRACT_P (decl))
     123              :     return false;
     124              : 
     125              :   /* We are concerned only about static/external vars and functions.  */
     126      1504635 :   if ((!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
     127      5565935 :       || !VAR_OR_FUNCTION_DECL_P (decl))
     128              :     return true;
     129              : 
     130              :   /* Static objects can be referred only if they are defined and not optimized
     131              :      out yet.  */
     132      4061300 :   if (!TREE_PUBLIC (decl))
     133              :     {
     134      1103792 :       if (DECL_EXTERNAL (decl))
     135              :         return false;
     136              :       /* Before we start optimizing unreachable code we can be sure all
     137              :          static objects are defined.  */
     138      1103747 :       if (symtab->function_flags_ready)
     139              :         return true;
     140      1064043 :       snode = symtab_node::get (decl);
     141      1064043 :       if (!snode || !snode->definition)
     142              :         return false;
     143      1063988 :       node = dyn_cast <cgraph_node *> (snode);
     144         9047 :       return !node || !node->inlined_to;
     145              :     }
     146              : 
     147              :   /* We will later output the initializer, so we can refer to it.
     148              :      So we are concerned only when DECL comes from initializer of
     149              :      external var or var that has been optimized out.  */
     150      2957508 :   if (!from_decl
     151       495844 :       || !VAR_P (from_decl)
     152       494715 :       || (!DECL_EXTERNAL (from_decl)
     153       221032 :           && (vnode = varpool_node::get (from_decl)) != NULL
     154       156193 :           && vnode->definition)
     155      3296036 :       || (flag_ltrans
     156            3 :           && (vnode = varpool_node::get (from_decl)) != NULL
     157            3 :           && vnode->in_other_partition))
     158              :     return true;
     159              :   /* We are folding reference from external vtable.  The vtable may refer
     160              :      to a symbol keyed to other compilation unit.  The other compilation
     161              :      unit may be in separate DSO and the symbol may be hidden.  */
     162       338525 :   if (DECL_VISIBILITY_SPECIFIED (decl)
     163       331451 :       && DECL_EXTERNAL (decl)
     164       266341 :       && DECL_VISIBILITY (decl) != VISIBILITY_DEFAULT
     165       519749 :       && (!(snode = symtab_node::get (decl)) || !snode->in_other_partition))
     166              :     return false;
     167              :   /* When function is public, we always can introduce new reference.
     168              :      Exception are the COMDAT functions where introducing a direct
     169              :      reference imply need to include function body in the current unit.  */
     170       157301 :   if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
     171              :     return true;
     172              :   /* We have COMDAT.  We are going to check if we still have definition
     173              :      or if the definition is going to be output in other partition.
     174              :      Bypass this when gimplifying; all needed functions will be produced.
     175              : 
     176              :      As observed in PR20991 for already optimized out comdat virtual functions
     177              :      it may be tempting to not necessarily give up because the copy will be
     178              :      output elsewhere when corresponding vtable is output.
     179              :      This is however not possible - ABI specify that COMDATs are output in
     180              :      units where they are used and when the other unit was compiled with LTO
     181              :      it is possible that vtable was kept public while the function itself
     182              :      was privatized. */
     183       110845 :   if (!symtab->function_flags_ready)
     184              :     return true;
     185              : 
     186        98021 :   snode = symtab_node::get (decl);
     187        98021 :   if (!snode
     188        98021 :       || ((!snode->definition || DECL_EXTERNAL (decl))
     189        12261 :           && (!snode->in_other_partition
     190            0 :               || (!snode->forced_by_abi && !snode->force_output))))
     191              :     return false;
     192        60034 :   node = dyn_cast <cgraph_node *> (snode);
     193        60034 :   return !node || !node->inlined_to;
     194              : }
     195              : 
     196              : /* CVAL is value taken from DECL_INITIAL of variable.  Try to transform it into
     197              :    acceptable form for is_gimple_min_invariant.
     198              :    FROM_DECL (if non-NULL) specify variable whose constructor contains CVAL.  */
     199              : 
     200              : tree
     201     16011878 : canonicalize_constructor_val (tree cval, tree from_decl)
     202              : {
     203     16011878 :   if (CONSTANT_CLASS_P (cval))
     204              :     return cval;
     205              : 
     206      9463862 :   tree orig_cval = cval;
     207      9463862 :   STRIP_NOPS (cval);
     208      9463862 :   if (TREE_CODE (cval) == POINTER_PLUS_EXPR
     209      9463862 :       && TREE_CODE (TREE_OPERAND (cval, 1)) == INTEGER_CST)
     210              :     {
     211        71895 :       tree ptr = TREE_OPERAND (cval, 0);
     212        71895 :       if (is_gimple_min_invariant (ptr))
     213       214974 :         cval = build1_loc (EXPR_LOCATION (cval),
     214        71658 :                            ADDR_EXPR, TREE_TYPE (ptr),
     215       143316 :                            fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (ptr)),
     216              :                                         ptr,
     217              :                                         fold_convert (ptr_type_node,
     218              :                                                       TREE_OPERAND (cval, 1))));
     219              :     }
     220      9463862 :   if (TREE_CODE (cval) == ADDR_EXPR)
     221              :     {
     222      5189635 :       tree base = NULL_TREE;
     223      5189635 :       if (TREE_CODE (TREE_OPERAND (cval, 0)) == COMPOUND_LITERAL_EXPR)
     224              :         {
     225          193 :           base = COMPOUND_LITERAL_EXPR_DECL (TREE_OPERAND (cval, 0));
     226          193 :           if (base)
     227          193 :             TREE_OPERAND (cval, 0) = base;
     228              :         }
     229              :       else
     230      5189442 :         base = get_base_address (TREE_OPERAND (cval, 0));
     231      5189635 :       if (!base)
     232              :         return NULL_TREE;
     233              : 
     234      2301407 :       if (VAR_OR_FUNCTION_DECL_P (base)
     235      6491576 :           && !can_refer_decl_in_current_unit_p (base, from_decl))
     236              :         return NULL_TREE;
     237      5007629 :       if (TREE_TYPE (base) == error_mark_node)
     238              :         return NULL_TREE;
     239      5007629 :       if (VAR_P (base))
     240              :         /* ???  We should be able to assert that TREE_ADDRESSABLE is set,
     241              :            but since the use can be in a debug stmt we can't.  */
     242              :         ;
     243      2300522 :       else if (TREE_CODE (base) == FUNCTION_DECL)
     244              :         {
     245              :           /* Make sure we create a cgraph node for functions we'll reference.
     246              :              They can be non-existent if the reference comes from an entry
     247              :              of an external vtable for example.  */
     248      1301056 :           cgraph_node::get_create (base);
     249              :         }
     250              :       /* Fixup types in global initializers.  */
     251      5007629 :       if (TREE_TYPE (TREE_TYPE (cval)) != TREE_TYPE (TREE_OPERAND (cval, 0)))
     252        40818 :         cval = build_fold_addr_expr (TREE_OPERAND (cval, 0));
     253              : 
     254      5007629 :       if (!useless_type_conversion_p (TREE_TYPE (orig_cval), TREE_TYPE (cval)))
     255       216950 :         cval = fold_convert (TREE_TYPE (orig_cval), cval);
     256              :       return cval;
     257              :     }
     258              :   /* In CONSTRUCTORs we may see unfolded constants like (int (*) ()) 0.  */
     259      4274227 :   if (TREE_CODE (cval) == INTEGER_CST)
     260              :     {
     261        65508 :       if (TREE_OVERFLOW_P (cval))
     262            0 :         cval = drop_tree_overflow (cval);
     263        65508 :       if (!useless_type_conversion_p (TREE_TYPE (orig_cval), TREE_TYPE (cval)))
     264        62307 :         cval = fold_convert (TREE_TYPE (orig_cval), cval);
     265              :       return cval;
     266              :     }
     267              :   return orig_cval;
     268              : }
     269              : 
     270              : /* If SYM is a constant variable with known value, return the value.
     271              :    NULL_TREE is returned otherwise.  */
     272              : 
     273              : tree
     274     20763190 : get_symbol_constant_value (tree sym)
     275              : {
     276     20763190 :   tree val = ctor_for_folding (sym);
     277     20763190 :   if (val != error_mark_node)
     278              :     {
     279        40107 :       if (val)
     280              :         {
     281        37800 :           val = canonicalize_constructor_val (unshare_expr (val), sym);
     282        37800 :           if (val
     283        37800 :               && is_gimple_min_invariant (val)
     284        66733 :               && useless_type_conversion_p (TREE_TYPE (sym), TREE_TYPE (val)))
     285              :             return val;
     286              :           else
     287         8973 :             return NULL_TREE;
     288              :         }
     289              :       /* Variables declared 'const' without an initializer
     290              :          have zero as the initializer if they may not be
     291              :          overridden at link or run time.  */
     292         2307 :       if (!val
     293         2307 :           && is_gimple_reg_type (TREE_TYPE (sym)))
     294         1970 :         return build_zero_cst (TREE_TYPE (sym));
     295              :     }
     296              : 
     297              :   return NULL_TREE;
     298              : }
     299              : 
     300              : 
     301              : 
     302              : /* Subroutine of fold_stmt.  We perform constant folding of the
     303              :    memory reference tree EXPR.  */
     304              : 
     305              : static tree
     306     66416133 : maybe_fold_reference (tree expr)
     307              : {
     308     66416133 :   tree result = NULL_TREE;
     309              : 
     310              :   /* Avoid expensive fold_const_aggregate_ref early on aggregate loads
     311              :      and esp. replacing STRING_CSTs inline.  */
     312     66416133 :   if (!is_gimple_reg_type (TREE_TYPE (expr)))
     313              :     return NULL_TREE;
     314              : 
     315     63258118 :   if ((TREE_CODE (expr) == VIEW_CONVERT_EXPR
     316     61907908 :        || TREE_CODE (expr) == REALPART_EXPR
     317     61092779 :        || TREE_CODE (expr) == IMAGPART_EXPR)
     318     65022485 :       && CONSTANT_CLASS_P (TREE_OPERAND (expr, 0)))
     319         5827 :     result = fold_unary_loc (EXPR_LOCATION (expr),
     320              :                              TREE_CODE (expr),
     321         5827 :                              TREE_TYPE (expr),
     322         5827 :                              TREE_OPERAND (expr, 0));
     323     63252291 :   else if (TREE_CODE (expr) == BIT_FIELD_REF
     324     63252291 :            && CONSTANT_CLASS_P (TREE_OPERAND (expr, 0)))
     325           49 :     result = fold_ternary_loc (EXPR_LOCATION (expr),
     326              :                                TREE_CODE (expr),
     327           49 :                                TREE_TYPE (expr),
     328           49 :                                TREE_OPERAND (expr, 0),
     329           49 :                                TREE_OPERAND (expr, 1),
     330           49 :                                TREE_OPERAND (expr, 2));
     331              :   else
     332     63252242 :     result = fold_const_aggregate_ref (expr);
     333              : 
     334     63258118 :   if (result && is_gimple_min_invariant (result))
     335       113977 :     return result;
     336              : 
     337              :   return NULL_TREE;
     338              : }
     339              : 
     340              : /* Return true if EXPR is an acceptable right-hand-side for a
     341              :    GIMPLE assignment.  We validate the entire tree, not just
     342              :    the root node, thus catching expressions that embed complex
     343              :    operands that are not permitted in GIMPLE.  This function
     344              :    is needed because the folding routines in fold-const.cc
     345              :    may return such expressions in some cases, e.g., an array
     346              :    access with an embedded index addition.  It may make more
     347              :    sense to have folding routines that are sensitive to the
     348              :    constraints on GIMPLE operands, rather than abandoning any
     349              :    any attempt to fold if the usual folding turns out to be too
     350              :    aggressive.  */
     351              : 
     352              : bool
     353            0 : valid_gimple_rhs_p (tree expr)
     354              : {
     355            0 :   enum tree_code code = TREE_CODE (expr);
     356              : 
     357            0 :   switch (TREE_CODE_CLASS (code))
     358              :     {
     359            0 :     case tcc_declaration:
     360            0 :       if (!is_gimple_variable (expr))
     361              :         return false;
     362              :       break;
     363              : 
     364              :     case tcc_constant:
     365              :       /* All constants are ok.  */
     366              :       break;
     367              : 
     368            0 :     case tcc_comparison:
     369              :       /* GENERIC allows comparisons with non-boolean types, reject
     370              :          those for GIMPLE.  Let vector-typed comparisons pass - rules
     371              :          for GENERIC and GIMPLE are the same here.  */
     372            0 :       if (!(INTEGRAL_TYPE_P (TREE_TYPE (expr))
     373            0 :             && (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE
     374            0 :                 || TYPE_PRECISION (TREE_TYPE (expr)) == 1))
     375            0 :           && ! VECTOR_TYPE_P (TREE_TYPE (expr)))
     376              :         return false;
     377              : 
     378              :       /* Fallthru.  */
     379            0 :     case tcc_binary:
     380            0 :       if (!is_gimple_val (TREE_OPERAND (expr, 0))
     381            0 :           || !is_gimple_val (TREE_OPERAND (expr, 1)))
     382              :         return false;
     383              :       break;
     384              : 
     385            0 :     case tcc_unary:
     386            0 :       if (!is_gimple_val (TREE_OPERAND (expr, 0)))
     387              :         return false;
     388              :       break;
     389              : 
     390            0 :     case tcc_expression:
     391            0 :       switch (code)
     392              :         {
     393            0 :         case ADDR_EXPR:
     394            0 :           {
     395            0 :             tree t;
     396            0 :             if (is_gimple_min_invariant (expr))
     397              :               return true;
     398            0 :             t = TREE_OPERAND (expr, 0);
     399            0 :             while (handled_component_p (t))
     400              :               {
     401              :                 /* ??? More checks needed, see the GIMPLE verifier.  */
     402            0 :                 if ((TREE_CODE (t) == ARRAY_REF
     403            0 :                      || TREE_CODE (t) == ARRAY_RANGE_REF)
     404            0 :                     && !is_gimple_val (TREE_OPERAND (t, 1)))
     405              :                   return false;
     406            0 :                 t = TREE_OPERAND (t, 0);
     407              :               }
     408            0 :             if (!is_gimple_id (t))
     409              :               return false;
     410              :           }
     411              :           break;
     412              : 
     413            0 :         default:
     414            0 :           if (get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS)
     415              :             {
     416            0 :               if (!is_gimple_val (TREE_OPERAND (expr, 0))
     417            0 :                   || !is_gimple_val (TREE_OPERAND (expr, 1))
     418            0 :                   || !is_gimple_val (TREE_OPERAND (expr, 2)))
     419              :                 return false;
     420              :               break;
     421              :             }
     422              :           return false;
     423              :         }
     424              :       break;
     425              : 
     426              :     case tcc_vl_exp:
     427              :       return false;
     428              : 
     429            0 :     case tcc_exceptional:
     430            0 :       if (code == CONSTRUCTOR)
     431              :         {
     432              :           unsigned i;
     433              :           tree elt;
     434            0 :           FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), i, elt)
     435            0 :             if (!is_gimple_val (elt))
     436              :               return false;
     437              :           return true;
     438              :         }
     439            0 :       if (code != SSA_NAME)
     440              :         return false;
     441              :       break;
     442              : 
     443            0 :     case tcc_reference:
     444            0 :       if (code == BIT_FIELD_REF)
     445            0 :         return is_gimple_val (TREE_OPERAND (expr, 0));
     446              :       return false;
     447              : 
     448              :     default:
     449              :       return false;
     450              :     }
     451              : 
     452              :   return true;
     453              : }
     454              : 
     455              : 
     456              : /* Attempt to fold an assignment statement pointed-to by SI.  Returns a
     457              :    replacement rhs for the statement or NULL_TREE if no simplification
     458              :    could be made.  It is assumed that the operands have been previously
     459              :    folded.  */
     460              : 
     461              : static tree
     462    266087604 : fold_gimple_assign (gimple_stmt_iterator *si)
     463              : {
     464    266087604 :   gimple *stmt = gsi_stmt (*si);
     465    266087604 :   enum tree_code subcode = gimple_assign_rhs_code (stmt);
     466    266087604 :   location_t loc = gimple_location (stmt);
     467              : 
     468    266087604 :   tree result = NULL_TREE;
     469              : 
     470    266087604 :   switch (get_gimple_rhs_class (subcode))
     471              :     {
     472    176303427 :     case GIMPLE_SINGLE_RHS:
     473    176303427 :       {
     474    176303427 :         tree rhs = gimple_assign_rhs1 (stmt);
     475              : 
     476    176303427 :         if (TREE_CLOBBER_P (rhs))
     477              :           return NULL_TREE;
     478              : 
     479    161957116 :         if (REFERENCE_CLASS_P (rhs))
     480     63779219 :           return maybe_fold_reference (rhs);
     481              : 
     482     98177897 :         else if (TREE_CODE (rhs) == OBJ_TYPE_REF)
     483              :           {
     484       166661 :             tree val = OBJ_TYPE_REF_EXPR (rhs);
     485       166661 :             if (is_gimple_min_invariant (val))
     486              :               return val;
     487       166638 :             else if (flag_devirtualize && virtual_method_call_p (rhs))
     488              :               {
     489       166598 :                 bool final;
     490       166598 :                 vec <cgraph_node *>targets
     491       166598 :                   = possible_polymorphic_call_targets (rhs, stmt, &final);
     492       166870 :                 if (final && targets.length () <= 1 && dbg_cnt (devirt))
     493              :                   {
     494           42 :                     if (dump_enabled_p ())
     495              :                       {
     496            0 :                         dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, stmt,
     497              :                                          "resolving virtual function address "
     498              :                                          "reference to function %s\n",
     499            0 :                                          targets.length () == 1
     500            0 :                                          ? targets[0]->name ()
     501              :                                          : "NULL");
     502              :                       }
     503           42 :                     if (targets.length () == 1)
     504              :                       {
     505           33 :                         val = fold_convert (TREE_TYPE (val),
     506              :                                             build_fold_addr_expr_loc
     507              :                                               (loc, targets[0]->decl));
     508           33 :                         STRIP_USELESS_TYPE_CONVERSION (val);
     509              :                       }
     510              :                     else
     511              :                       /* We cannot use __builtin_unreachable here because it
     512              :                          cannot have address taken.  */
     513            9 :                       val = build_int_cst (TREE_TYPE (val), 0);
     514           42 :                     return val;
     515              :                   }
     516              :               }
     517              :           }
     518              : 
     519     98011236 :         else if (TREE_CODE (rhs) == ADDR_EXPR)
     520              :           {
     521     15359091 :             tree ref = TREE_OPERAND (rhs, 0);
     522     15359091 :             if (TREE_CODE (ref) == MEM_REF
     523     15359091 :                 && integer_zerop (TREE_OPERAND (ref, 1)))
     524              :               {
     525         2680 :                 result = TREE_OPERAND (ref, 0);
     526         2680 :                 if (!useless_type_conversion_p (TREE_TYPE (rhs),
     527         2680 :                                                 TREE_TYPE (result)))
     528            0 :                   result = build1 (NOP_EXPR, TREE_TYPE (rhs), result);
     529              :                 return result;
     530              :               }
     531              :           }
     532              : 
     533     82652145 :         else if (TREE_CODE (rhs) == CONSTRUCTOR
     534     82652145 :                  && TREE_CODE (TREE_TYPE (rhs)) == VECTOR_TYPE)
     535              :           {
     536              :             /* Fold a constant vector CONSTRUCTOR to VECTOR_CST.  */
     537              :             unsigned i;
     538              :             tree val;
     539              : 
     540       472305 :             FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), i, val)
     541       467924 :               if (! CONSTANT_CLASS_P (val))
     542              :                 return NULL_TREE;
     543              : 
     544         4381 :             return build_vector_from_ctor (TREE_TYPE (rhs),
     545         8762 :                                            CONSTRUCTOR_ELTS (rhs));
     546              :           }
     547              : 
     548     82277704 :         else if (DECL_P (rhs)
     549     82277704 :                  && is_gimple_reg_type (TREE_TYPE (rhs)))
     550     12452172 :           return get_symbol_constant_value (rhs);
     551              :       }
     552              :       break;
     553              : 
     554              :     case GIMPLE_UNARY_RHS:
     555              :       break;
     556              : 
     557              :     case GIMPLE_BINARY_RHS:
     558              :       break;
     559              : 
     560       568343 :     case GIMPLE_TERNARY_RHS:
     561      1136686 :       result = fold_ternary_loc (loc, subcode,
     562       568343 :                                  TREE_TYPE (gimple_assign_lhs (stmt)),
     563              :                                  gimple_assign_rhs1 (stmt),
     564              :                                  gimple_assign_rhs2 (stmt),
     565              :                                  gimple_assign_rhs3 (stmt));
     566              : 
     567       568343 :       if (result)
     568              :         {
     569            0 :           STRIP_USELESS_TYPE_CONVERSION (result);
     570            0 :           if (valid_gimple_rhs_p (result))
     571            0 :             return result;
     572              :         }
     573              :       break;
     574              : 
     575            0 :     case GIMPLE_INVALID_RHS:
     576            0 :       gcc_unreachable ();
     577              :     }
     578              : 
     579              :   return NULL_TREE;
     580              : }
     581              : 
     582              : 
     583              : /* Replace a statement at *SI_P with a sequence of statements in STMTS,
     584              :    adjusting the replacement stmts location and virtual operands.
     585              : 
     586              :    If the statements has an lhs, either:
     587              : 
     588              :    - the last statement of the new sequence must assign to the same lhs or
     589              : 
     590              :    - the caller must ensure that all uses of the old lhs have been
     591              :      removed before calling this function.  This includes removing
     592              :      all debug uses.  */
     593              : 
     594              : void
     595       129299 : gsi_replace_with_seq_vops (gimple_stmt_iterator *si_p, gimple_seq stmts)
     596              : {
     597       129299 :   gimple *stmt = gsi_stmt (*si_p);
     598              : 
     599       129299 :   if (gimple_has_location (stmt))
     600       107117 :     annotate_all_with_location (stmts, gimple_location (stmt));
     601              : 
     602              :   /* First iterate over the replacement statements backward, assigning
     603              :      virtual operands to their defining statements.  */
     604       129299 :   gimple *laststore = NULL;
     605       258598 :   for (gimple_stmt_iterator i = gsi_last (stmts);
     606       458952 :        !gsi_end_p (i); gsi_prev (&i))
     607              :     {
     608       200354 :       gimple *new_stmt = gsi_stmt (i);
     609       200354 :       if ((gimple_assign_single_p (new_stmt)
     610       126310 :            && !is_gimple_reg (gimple_assign_lhs (new_stmt)))
     611       326370 :           || (is_gimple_call (new_stmt)
     612        14123 :               && (gimple_call_flags (new_stmt)
     613        14123 :                   & (ECF_NOVOPS | ECF_PURE | ECF_CONST | ECF_NORETURN)) == 0))
     614              :         {
     615         2438 :           tree vdef;
     616         2438 :           if (!laststore)
     617         2432 :             vdef = gimple_vdef (stmt);
     618              :           else
     619            6 :             vdef = make_ssa_name (gimple_vop (cfun), new_stmt);
     620         2438 :           gimple_set_vdef (new_stmt, vdef);
     621         2438 :           if (vdef && TREE_CODE (vdef) == SSA_NAME)
     622         1418 :             SSA_NAME_DEF_STMT (vdef) = new_stmt;
     623              :           laststore = new_stmt;
     624              :         }
     625              :     }
     626              : 
     627              :   /* Second iterate over the statements forward, assigning virtual
     628              :      operands to their uses.  */
     629       129299 :   tree reaching_vuse = gimple_vuse (stmt);
     630       129299 :   for (gimple_stmt_iterator i = gsi_start (stmts);
     631       329653 :        !gsi_end_p (i); gsi_next (&i))
     632              :     {
     633       200354 :       gimple *new_stmt = gsi_stmt (i);
     634              :       /* If the new statement possibly has a VUSE, update it with exact SSA
     635              :          name we know will reach this one.  */
     636       200354 :       if (gimple_has_mem_ops (new_stmt))
     637       200352 :         gimple_set_vuse (new_stmt, reaching_vuse);
     638       200354 :       gimple_set_modified (new_stmt, true);
     639       599636 :       if (gimple_vdef (new_stmt))
     640              :         reaching_vuse = gimple_vdef (new_stmt);
     641              :     }
     642              : 
     643              :   /* If the new sequence does not do a store release the virtual
     644              :      definition of the original statement.  */
     645       129299 :   if (reaching_vuse
     646       214746 :       && reaching_vuse == gimple_vuse (stmt))
     647              :     {
     648        84035 :       tree vdef = gimple_vdef (stmt);
     649        84035 :       if (vdef
     650         1541 :           && TREE_CODE (vdef) == SSA_NAME)
     651              :         {
     652         1485 :           unlink_stmt_vdef (stmt);
     653         1485 :           release_ssa_name (vdef);
     654              :         }
     655              :     }
     656              : 
     657              :   /* Finally replace the original statement with the sequence.  */
     658       129299 :   gsi_replace_with_seq (si_p, stmts, false);
     659       129299 : }
     660              : 
     661              : /* Helper function for update_gimple_call and
     662              :    gimplify_and_update_call_from_tree.  A GIMPLE_CALL STMT is being replaced
     663              :    with GIMPLE_CALL NEW_STMT.  */
     664              : 
     665              : static void
     666         2479 : finish_update_gimple_call (gimple_stmt_iterator *si_p, gimple *new_stmt,
     667              :                            gimple *stmt)
     668              : {
     669         2479 :   tree lhs = gimple_call_lhs (stmt);
     670         2479 :   gimple_call_set_lhs (new_stmt, lhs);
     671         2479 :   if (lhs && TREE_CODE (lhs) == SSA_NAME)
     672          810 :     SSA_NAME_DEF_STMT (lhs) = new_stmt;
     673         2479 :   gimple_move_vops (new_stmt, stmt);
     674         2479 :   gimple_set_location (new_stmt, gimple_location (stmt));
     675         2479 :   if (gimple_block (new_stmt) == NULL_TREE)
     676            1 :     gimple_set_block (new_stmt, gimple_block (stmt));
     677         2479 :   gsi_replace (si_p, new_stmt, false);
     678         2479 : }
     679              : 
     680              : /* Update a GIMPLE_CALL statement at iterator *SI_P to call to FN
     681              :    with number of arguments NARGS, where the arguments in GIMPLE form
     682              :    follow NARGS argument.  */
     683              : 
     684              : bool
     685         2476 : update_gimple_call (gimple_stmt_iterator *si_p, tree fn, int nargs, ...)
     686              : {
     687         2476 :   va_list ap;
     688         2476 :   gcall *new_stmt, *stmt = as_a <gcall *> (gsi_stmt (*si_p));
     689              : 
     690         2476 :   gcc_assert (is_gimple_call (stmt));
     691         2476 :   va_start (ap, nargs);
     692         2476 :   new_stmt = gimple_build_call_valist (fn, nargs, ap);
     693         2476 :   finish_update_gimple_call (si_p, new_stmt, stmt);
     694         2476 :   va_end (ap);
     695         2476 :   return true;
     696              : }
     697              : 
     698              : /* Return true if EXPR is a CALL_EXPR suitable for representation
     699              :    as a single GIMPLE_CALL statement.  If the arguments require
     700              :    further gimplification, return false.  */
     701              : 
     702              : static bool
     703        60439 : valid_gimple_call_p (tree expr)
     704              : {
     705        60439 :   unsigned i, nargs;
     706              : 
     707        60439 :   if (TREE_CODE (expr) != CALL_EXPR)
     708              :     return false;
     709              : 
     710            3 :   nargs = call_expr_nargs (expr);
     711            6 :   for (i = 0; i < nargs; i++)
     712              :     {
     713            3 :       tree arg = CALL_EXPR_ARG (expr, i);
     714            3 :       if (is_gimple_reg_type (TREE_TYPE (arg)))
     715              :         {
     716            3 :           if (!is_gimple_val (arg))
     717              :             return false;
     718              :         }
     719              :       else
     720            0 :         if (!is_gimple_lvalue (arg))
     721              :           return false;
     722              :     }
     723              : 
     724              :   return true;
     725              : }
     726              : 
     727              : /* Convert EXPR into a GIMPLE value suitable for substitution on the
     728              :    RHS of an assignment.  Insert the necessary statements before
     729              :    iterator *SI_P.  The statement at *SI_P, which must be a GIMPLE_CALL
     730              :    is replaced.  If the call is expected to produces a result, then it
     731              :    is replaced by an assignment of the new RHS to the result variable.
     732              :    If the result is to be ignored, then the call is replaced by a
     733              :    GIMPLE_NOP.  A proper VDEF chain is retained by making the first
     734              :    VUSE and the last VDEF of the whole sequence be the same as the replaced
     735              :    statement and using new SSA names for stores in between.  */
     736              : 
     737              : void
     738        60439 : gimplify_and_update_call_from_tree (gimple_stmt_iterator *si_p, tree expr)
     739              : {
     740        60439 :   tree lhs;
     741        60439 :   gimple *stmt, *new_stmt;
     742        60439 :   gimple_stmt_iterator i;
     743        60439 :   gimple_seq stmts = NULL;
     744              : 
     745        60439 :   stmt = gsi_stmt (*si_p);
     746              : 
     747        60439 :   gcc_assert (is_gimple_call (stmt));
     748              : 
     749        60439 :   if (valid_gimple_call_p (expr))
     750              :     {
     751              :       /* The call has simplified to another call.  */
     752            3 :       tree fn = CALL_EXPR_FN (expr);
     753            3 :       unsigned i;
     754            3 :       unsigned nargs = call_expr_nargs (expr);
     755            3 :       vec<tree> args = vNULL;
     756            3 :       gcall *new_stmt;
     757              : 
     758            3 :       if (nargs > 0)
     759              :         {
     760            3 :           args.create (nargs);
     761            3 :           args.safe_grow_cleared (nargs, true);
     762              : 
     763            9 :           for (i = 0; i < nargs; i++)
     764            3 :             args[i] = CALL_EXPR_ARG (expr, i);
     765              :         }
     766              : 
     767            3 :       new_stmt = gimple_build_call_vec (fn, args);
     768            3 :       finish_update_gimple_call (si_p, new_stmt, stmt);
     769            3 :       args.release ();
     770            3 :       return;
     771              :     }
     772              : 
     773        60436 :   lhs = gimple_call_lhs (stmt);
     774        60436 :   if (lhs == NULL_TREE)
     775              :     {
     776         2570 :       push_gimplify_context (gimple_in_ssa_p (cfun));
     777         1285 :       gimplify_and_add (expr, &stmts);
     778         1285 :       pop_gimplify_context (NULL);
     779              : 
     780              :       /* We can end up with folding a memcpy of an empty class assignment
     781              :          which gets optimized away by C++ gimplification.  */
     782         1285 :       if (gimple_seq_empty_p (stmts))
     783              :         {
     784         1126 :           if (gimple_in_ssa_p (cfun))
     785              :             {
     786         1126 :               unlink_stmt_vdef (stmt);
     787         1126 :               release_defs (stmt);
     788              :             }
     789         1126 :           gsi_replace (si_p, gimple_build_nop (), false);
     790         1126 :           return;
     791              :         }
     792              :     }
     793              :   else
     794              :     {
     795        59151 :       tree tmp = force_gimple_operand (expr, &stmts, false, NULL_TREE);
     796        59151 :       new_stmt = gimple_build_assign (lhs, tmp);
     797        59151 :       i = gsi_last (stmts);
     798        59151 :       gsi_insert_after_without_update (&i, new_stmt,
     799              :                                        GSI_CONTINUE_LINKING);
     800              :     }
     801              : 
     802        59310 :   gsi_replace_with_seq_vops (si_p, stmts);
     803              : }
     804              : 
     805              : /* Print a message in the dump file recording transformation of FROM to TO.  */
     806              : 
     807              : static void
     808        40736 : dump_transformation (gcall *from, gcall *to)
     809              : {
     810        40736 :   if (dump_enabled_p ())
     811           12 :     dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, from, "simplified %T to %T\n",
     812              :                      gimple_call_fn (from), gimple_call_fn (to));
     813        40736 : }
     814              : 
     815              : /* Replace the call at *GSI with the gimple value VAL.  */
     816              : 
     817              : void
     818        87244 : replace_call_with_value (gimple_stmt_iterator *gsi, tree val)
     819              : {
     820        87244 :   gimple *stmt = gsi_stmt (*gsi);
     821        87244 :   tree lhs = gimple_call_lhs (stmt);
     822        87244 :   gimple *repl;
     823        87244 :   if (lhs)
     824              :     {
     825        82065 :       if (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (val)))
     826         2210 :         val = fold_convert (TREE_TYPE (lhs), val);
     827        82065 :       repl = gimple_build_assign (lhs, val);
     828              :     }
     829              :   else
     830         5179 :     repl = gimple_build_nop ();
     831        87244 :   tree vdef = gimple_vdef (stmt);
     832        87244 :   if (vdef && TREE_CODE (vdef) == SSA_NAME)
     833              :     {
     834         5744 :       unlink_stmt_vdef (stmt);
     835         5744 :       release_ssa_name (vdef);
     836              :     }
     837        87244 :   gsi_replace (gsi, repl, false);
     838        87244 : }
     839              : 
     840              : /* Replace the call at *GSI with the new call REPL and fold that
     841              :    again.  */
     842              : 
     843              : static void
     844        40736 : replace_call_with_call_and_fold (gimple_stmt_iterator *gsi, gimple *repl)
     845              : {
     846        40736 :   gimple *stmt = gsi_stmt (*gsi);
     847        40736 :   dump_transformation (as_a <gcall *> (stmt), as_a <gcall *> (repl));
     848        40736 :   gimple_call_set_lhs (repl, gimple_call_lhs (stmt));
     849        40736 :   gimple_set_location (repl, gimple_location (stmt));
     850        40736 :   gimple_move_vops (repl, stmt);
     851        40736 :   gsi_replace (gsi, repl, false);
     852        40736 :   fold_stmt (gsi);
     853        40736 : }
     854              : 
     855              : /* Return true if VAR is a VAR_DECL or a component thereof.  */
     856              : 
     857              : static bool
     858       412823 : var_decl_component_p (tree var)
     859              : {
     860       412823 :   tree inner = var;
     861       580250 :   while (handled_component_p (inner))
     862       167427 :     inner = TREE_OPERAND (inner, 0);
     863       412823 :   return (DECL_P (inner)
     864       412823 :           || (TREE_CODE (inner) == MEM_REF
     865        43832 :               && TREE_CODE (TREE_OPERAND (inner, 0)) == ADDR_EXPR));
     866              : }
     867              : 
     868              : /* Return TRUE if the SIZE argument, representing the size of an
     869              :    object, is in a range of values of which exactly zero is valid.  */
     870              : 
     871              : static bool
     872      1037712 : size_must_be_zero_p (tree size)
     873              : {
     874      1037712 :   if (integer_zerop (size))
     875              :     return true;
     876              : 
     877      1035045 :   if (TREE_CODE (size) != SSA_NAME || !INTEGRAL_TYPE_P (TREE_TYPE (size)))
     878              :     return false;
     879              : 
     880       627527 :   tree type = TREE_TYPE (size);
     881       627527 :   int prec = TYPE_PRECISION (type);
     882              : 
     883              :   /* Compute the value of SSIZE_MAX, the largest positive value that
     884              :      can be stored in ssize_t, the signed counterpart of size_t.  */
     885       627527 :   wide_int ssize_max = wi::lshift (wi::one (prec), prec - 1) - 1;
     886       627527 :   wide_int zero = wi::zero (TYPE_PRECISION (type));
     887       627527 :   int_range_max valid_range (type, zero, ssize_max);
     888       627527 :   int_range_max vr;
     889      1255054 :   get_range_query (cfun)->range_of_expr (vr, size);
     890              : 
     891       627527 :   if (vr.undefined_p ())
     892           85 :     vr.set_varying (TREE_TYPE (size));
     893       627527 :   vr.intersect (valid_range);
     894       627527 :   return vr.zero_p ();
     895       627527 : }
     896              : 
     897              : /* Fold function call to builtin mem{{,p}cpy,move}.  Try to detect and
     898              :    diagnose (otherwise undefined) overlapping copies without preventing
     899              :    folding.  When folded, GCC guarantees that overlapping memcpy has
     900              :    the same semantics as memmove.  Call to the library memcpy need not
     901              :    provide the same guarantee.  Return false if no simplification can
     902              :    be made.  */
     903              : 
     904              : static bool
     905      1037712 : gimple_fold_builtin_memory_op (gimple_stmt_iterator *gsi,
     906              :                                tree dest, tree src, enum built_in_function code)
     907              : {
     908      1037712 :   gimple *stmt = gsi_stmt (*gsi);
     909      1037712 :   tree lhs = gimple_call_lhs (stmt);
     910      1037712 :   tree len = gimple_call_arg (stmt, 2);
     911      1037712 :   location_t loc = gimple_location (stmt);
     912              : 
     913              :   /* If the LEN parameter is a constant zero or in range where
     914              :      the only valid value is zero, return DEST.  */
     915      1037712 :   if (size_must_be_zero_p (len))
     916              :     {
     917         2719 :       gimple *repl;
     918         2719 :       if (gimple_call_lhs (stmt))
     919           60 :         repl = gimple_build_assign (gimple_call_lhs (stmt), dest);
     920              :       else
     921         2659 :         repl = gimple_build_nop ();
     922         2719 :       tree vdef = gimple_vdef (stmt);
     923         2719 :       if (vdef && TREE_CODE (vdef) == SSA_NAME)
     924              :         {
     925          504 :           unlink_stmt_vdef (stmt);
     926          504 :           release_ssa_name (vdef);
     927              :         }
     928         2719 :       gsi_replace (gsi, repl, false);
     929         2719 :       return true;
     930              :     }
     931              : 
     932              :   /* If SRC and DEST are the same (and not volatile), return
     933              :      DEST{,+LEN,+LEN-1}.  */
     934      1034993 :   if (operand_equal_p (src, dest, 0))
     935              :     {
     936              :       /* Avoid diagnosing exact overlap in calls to __builtin_memcpy.
     937              :          It's safe and may even be emitted by GCC itself (see bug
     938              :          32667).  */
     939           84 :       unlink_stmt_vdef (stmt);
     940          168 :       if (gimple_vdef (stmt) && TREE_CODE (gimple_vdef (stmt)) == SSA_NAME)
     941           49 :         release_ssa_name (gimple_vdef (stmt));
     942           84 :       if (!lhs)
     943              :         {
     944           63 :           gsi_replace (gsi, gimple_build_nop (), false);
     945           63 :           return true;
     946              :         }
     947           21 :       goto done;
     948              :     }
     949      2069818 :   else if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
     950              :     return false;
     951              :   else
     952              :     {
     953              :       /* We cannot (easily) change the type of the copy if it is a storage
     954              :          order barrier, i.e. is equivalent to a VIEW_CONVERT_EXPR that can
     955              :          modify the storage order of objects (see storage_order_barrier_p).  */
     956      1034909 :       tree srctype
     957      1049234 :         = POINTER_TYPE_P (TREE_TYPE (src))
     958      1049234 :           ? TREE_TYPE (TREE_TYPE (src)) : NULL_TREE;
     959      1034909 :       tree desttype
     960      1056392 :         = POINTER_TYPE_P (TREE_TYPE (dest))
     961      1056392 :           ? TREE_TYPE (TREE_TYPE (dest)) : NULL_TREE;
     962      1034909 :       tree destvar, srcvar, srcoff;
     963      1034909 :       unsigned int src_align, dest_align;
     964      1034909 :       unsigned HOST_WIDE_INT tmp_len;
     965      1034909 :       const char *tmp_str;
     966              : 
     967              :       /* Build accesses at offset zero with a ref-all character type.  */
     968      1034909 :       tree off0
     969      1034909 :         = build_int_cst (build_pointer_type_for_mode (char_type_node,
     970              :                                                       ptr_mode, true), 0);
     971              : 
     972              :       /* If we can perform the copy efficiently with first doing all loads
     973              :          and then all stores inline it that way.  Currently efficiently
     974              :          means that we can load all the memory into a single integer
     975              :          register which is what MOVE_MAX gives us.  */
     976      1034909 :       src_align = get_pointer_alignment (src);
     977      1034909 :       dest_align = get_pointer_alignment (dest);
     978      1034909 :       if (tree_fits_uhwi_p (len)
     979       397379 :           && compare_tree_int (len, MOVE_MAX) <= 0
     980              :           /* FIXME: Don't transform copies from strings with known length.
     981              :              Until GCC 9 this prevented a case in gcc.dg/strlenopt-8.c
     982              :              from being handled, and the case was XFAILed for that reason.
     983              :              Now that it is handled and the XFAIL removed, as soon as other
     984              :              strlenopt tests that rely on it for passing are adjusted, this
     985              :              hack can be removed.  */
     986       297617 :           && !c_strlen (src, 1)
     987       197171 :           && !((tmp_str = getbyterep (src, &tmp_len)) != NULL
     988        82351 :                && memchr (tmp_str, 0, tmp_len) == NULL)
     989       127994 :           && !(srctype
     990       127994 :                && AGGREGATE_TYPE_P (srctype)
     991        60475 :                && TYPE_REVERSE_STORAGE_ORDER (srctype))
     992      1162770 :           && !(desttype
     993       127861 :                && AGGREGATE_TYPE_P (desttype)
     994        70096 :                && TYPE_REVERSE_STORAGE_ORDER (desttype)))
     995              :         {
     996       127828 :           unsigned ilen = tree_to_uhwi (len);
     997       127828 :           if (pow2p_hwi (ilen))
     998              :             {
     999              :               /* Detect out-of-bounds accesses without issuing warnings.
    1000              :                  Avoid folding out-of-bounds copies but to avoid false
    1001              :                  positives for unreachable code defer warning until after
    1002              :                  DCE has worked its magic.
    1003              :                  -Wrestrict is still diagnosed.  */
    1004        26683 :               if (int warning = check_bounds_or_overlap (as_a <gcall *>(stmt),
    1005              :                                                          dest, src, len, len,
    1006        26683 :                                                          false, false))
    1007          991 :                 if (warning != OPT_Wrestrict)
    1008      1031362 :                   return false;
    1009              : 
    1010        25751 :               scalar_int_mode imode;
    1011        25751 :               machine_mode mode;
    1012        25751 :               if (int_mode_for_size (ilen * BITS_PER_UNIT, 0).exists (&imode)
    1013        51502 :                   && bitwise_mode_for_size (ilen
    1014        25751 :                                             * BITS_PER_UNIT).exists (&mode)
    1015        51502 :                   && known_eq (GET_MODE_BITSIZE (mode), ilen * BITS_PER_UNIT)
    1016              :                   /* If the destination pointer is not aligned we must be able
    1017              :                      to emit an unaligned store.  */
    1018        25751 :                   && (dest_align >= GET_MODE_ALIGNMENT (mode)
    1019        13634 :                       || !targetm.slow_unaligned_access (mode, dest_align)
    1020            0 :                       || (optab_handler (movmisalign_optab, mode)
    1021              :                           != CODE_FOR_nothing)))
    1022              :                 {
    1023        25751 :                   tree type = bitwise_type_for_mode (mode);
    1024        25751 :                   tree srctype = type;
    1025        25751 :                   tree desttype = type;
    1026        25751 :                   if (src_align < GET_MODE_ALIGNMENT (mode))
    1027        13153 :                     srctype = build_aligned_type (type, src_align);
    1028        25751 :                   tree srcmem = fold_build2 (MEM_REF, srctype, src, off0);
    1029        25751 :                   tree tem = fold_const_aggregate_ref (srcmem);
    1030        25751 :                   if (tem)
    1031              :                     srcmem = tem;
    1032        24602 :                   else if (src_align < GET_MODE_ALIGNMENT (mode)
    1033        12890 :                            && targetm.slow_unaligned_access (mode, src_align)
    1034        24602 :                            && (optab_handler (movmisalign_optab, mode)
    1035              :                                == CODE_FOR_nothing))
    1036              :                     srcmem = NULL_TREE;
    1037        24602 :                   if (srcmem)
    1038              :                     {
    1039        25751 :                       gassign *new_stmt;
    1040        25751 :                       if (is_gimple_reg_type (TREE_TYPE (srcmem)))
    1041              :                         {
    1042        25751 :                           new_stmt = gimple_build_assign (NULL_TREE, srcmem);
    1043        25751 :                           srcmem
    1044        25751 :                             = make_ssa_name (TREE_TYPE (srcmem), new_stmt);
    1045        25751 :                           gimple_assign_set_lhs (new_stmt, srcmem);
    1046        51502 :                           gimple_set_vuse (new_stmt, gimple_vuse (stmt));
    1047        25751 :                           gimple_set_location (new_stmt, loc);
    1048        25751 :                           gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
    1049              :                         }
    1050        25751 :                       if (dest_align < GET_MODE_ALIGNMENT (mode))
    1051        13634 :                         desttype = build_aligned_type (type, dest_align);
    1052        25751 :                       new_stmt
    1053        25751 :                         = gimple_build_assign (fold_build2 (MEM_REF, desttype,
    1054              :                                                             dest, off0),
    1055              :                                                srcmem);
    1056        25751 :                       gimple_move_vops (new_stmt, stmt);
    1057        25751 :                       if (!lhs)
    1058              :                         {
    1059        22388 :                           gsi_replace (gsi, new_stmt, false);
    1060        22388 :                           return true;
    1061              :                         }
    1062         3363 :                       gimple_set_location (new_stmt, loc);
    1063         3363 :                       gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
    1064         3363 :                       goto done;
    1065              :                     }
    1066              :                 }
    1067              :             }
    1068              :         }
    1069              : 
    1070      1008226 :       if (code == BUILT_IN_MEMMOVE)
    1071              :         {
    1072              :           /* Both DEST and SRC must be pointer types.
    1073              :              ??? This is what old code did.  Is the testing for pointer types
    1074              :              really mandatory?
    1075              : 
    1076              :              If either SRC is readonly or length is 1, we can use memcpy.  */
    1077       209274 :           if (!dest_align || !src_align)
    1078              :             return false;
    1079       209274 :           if (readonly_data_expr (src)
    1080       209274 :               || (tree_fits_uhwi_p (len)
    1081        33628 :                   && (MIN (src_align, dest_align) / BITS_PER_UNIT
    1082        33628 :                       >= tree_to_uhwi (len))))
    1083              :             {
    1084      1052889 :               tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
    1085        21527 :               if (!fn)
    1086              :                 return false;
    1087        21527 :               gimple_call_set_fndecl (stmt, fn);
    1088        21527 :               gimple_call_set_arg (stmt, 0, dest);
    1089        21527 :               gimple_call_set_arg (stmt, 1, src);
    1090        21527 :               fold_stmt (gsi);
    1091        21527 :               return true;
    1092              :             }
    1093              : 
    1094              :           /* If *src and *dest can't overlap, optimize into memcpy as well.  */
    1095       187747 :           if (TREE_CODE (src) == ADDR_EXPR
    1096         5691 :               && TREE_CODE (dest) == ADDR_EXPR)
    1097              :             {
    1098         2000 :               tree src_base, dest_base, fn;
    1099         2000 :               poly_int64 src_offset = 0, dest_offset = 0;
    1100         2000 :               poly_uint64 maxsize;
    1101              : 
    1102         2000 :               srcvar = TREE_OPERAND (src, 0);
    1103         2000 :               src_base = get_addr_base_and_unit_offset (srcvar, &src_offset);
    1104         2000 :               if (src_base == NULL)
    1105            0 :                 src_base = srcvar;
    1106         2000 :               destvar = TREE_OPERAND (dest, 0);
    1107         2000 :               dest_base = get_addr_base_and_unit_offset (destvar,
    1108              :                                                          &dest_offset);
    1109         2000 :               if (dest_base == NULL)
    1110            0 :                 dest_base = destvar;
    1111         2000 :               if (!poly_int_tree_p (len, &maxsize))
    1112          234 :                 maxsize = -1;
    1113         2000 :               if (SSA_VAR_P (src_base)
    1114         2000 :                   && SSA_VAR_P (dest_base))
    1115              :                 {
    1116         2000 :                   if (operand_equal_p (src_base, dest_base, 0)
    1117         2000 :                       && ranges_maybe_overlap_p (src_offset, maxsize,
    1118              :                                                  dest_offset, maxsize))
    1119              :                     return false;
    1120              :                 }
    1121            0 :               else if (TREE_CODE (src_base) == MEM_REF
    1122            0 :                        && TREE_CODE (dest_base) == MEM_REF)
    1123              :                 {
    1124            0 :                   if (! operand_equal_p (TREE_OPERAND (src_base, 0),
    1125            0 :                                          TREE_OPERAND (dest_base, 0), 0))
    1126            0 :                     return false;
    1127            0 :                   poly_offset_int full_src_offset
    1128            0 :                     = mem_ref_offset (src_base) + src_offset;
    1129            0 :                   poly_offset_int full_dest_offset
    1130            0 :                     = mem_ref_offset (dest_base) + dest_offset;
    1131            0 :                   if (ranges_maybe_overlap_p (full_src_offset, maxsize,
    1132              :                                               full_dest_offset, maxsize))
    1133              :                     return false;
    1134            0 :                 }
    1135              :               else
    1136              :                 return false;
    1137              : 
    1138         3408 :               fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
    1139         1408 :               if (!fn)
    1140              :                 return false;
    1141         1408 :               gimple_call_set_fndecl (stmt, fn);
    1142         1408 :               gimple_call_set_arg (stmt, 0, dest);
    1143         1408 :               gimple_call_set_arg (stmt, 1, src);
    1144         1408 :               fold_stmt (gsi);
    1145         1408 :               return true;
    1146              :             }
    1147              : 
    1148              :           /* If the destination and source do not alias optimize into
    1149              :              memcpy as well.  */
    1150       185747 :           if ((is_gimple_min_invariant (dest)
    1151       181920 :                || TREE_CODE (dest) == SSA_NAME)
    1152       348449 :               && (is_gimple_min_invariant (src)
    1153       162865 :                   || TREE_CODE (src) == SSA_NAME))
    1154              :             {
    1155       165957 :               ao_ref destr, srcr;
    1156       165957 :               ao_ref_init_from_ptr_and_size (&destr, dest, len);
    1157       165957 :               ao_ref_init_from_ptr_and_size (&srcr, src, len);
    1158       165957 :               if (!refs_may_alias_p_1 (&destr, &srcr, false))
    1159              :                 {
    1160        10492 :                   tree fn;
    1161        10492 :                   fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
    1162        10492 :                   if (!fn)
    1163        10492 :                     return false;
    1164        10492 :                   gimple_call_set_fndecl (stmt, fn);
    1165        10492 :                   gimple_call_set_arg (stmt, 0, dest);
    1166        10492 :                   gimple_call_set_arg (stmt, 1, src);
    1167        10492 :                   fold_stmt (gsi);
    1168        10492 :                   return true;
    1169              :                 }
    1170              :             }
    1171              : 
    1172              :           return false;
    1173              :         }
    1174              : 
    1175       798952 :       if (!tree_fits_shwi_p (len))
    1176              :         return false;
    1177       319573 :       if (!srctype
    1178       319573 :           || (AGGREGATE_TYPE_P (srctype)
    1179       195831 :               && TYPE_REVERSE_STORAGE_ORDER (srctype)))
    1180              :         return false;
    1181       319440 :       if (!desttype
    1182       319440 :           || (AGGREGATE_TYPE_P (desttype)
    1183       183751 :               && TYPE_REVERSE_STORAGE_ORDER (desttype)))
    1184              :         return false;
    1185              :       /* In the following try to find a type that is most natural to be
    1186              :          used for the memcpy source and destination and that allows
    1187              :          the most optimization when memcpy is turned into a plain assignment
    1188              :          using that type.  In theory we could always use a char[len] type
    1189              :          but that only gains us that the destination and source possibly
    1190              :          no longer will have their address taken.  */
    1191       319407 :       if (TREE_CODE (srctype) == ARRAY_TYPE
    1192       319407 :           && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
    1193       120660 :         srctype = TREE_TYPE (srctype);
    1194       319407 :       if (TREE_CODE (desttype) == ARRAY_TYPE
    1195       319407 :           && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
    1196        95381 :         desttype = TREE_TYPE (desttype);
    1197       319407 :       if (TREE_ADDRESSABLE (srctype)
    1198       319318 :           || TREE_ADDRESSABLE (desttype))
    1199              :         return false;
    1200              : 
    1201              :       /* Make sure we are not copying using a floating-point mode or
    1202              :          a type whose size possibly does not match its precision.  */
    1203       637904 :       if (FLOAT_MODE_P (TYPE_MODE (desttype))
    1204       318355 :           || TREE_CODE (desttype) == BOOLEAN_TYPE
    1205       637619 :           || TREE_CODE (desttype) == ENUMERAL_TYPE)
    1206          973 :         desttype = bitwise_type_for_mode (TYPE_MODE (desttype));
    1207       637823 :       if (FLOAT_MODE_P (TYPE_MODE (srctype))
    1208       318425 :           || TREE_CODE (srctype) == BOOLEAN_TYPE
    1209       637694 :           || TREE_CODE (srctype) == ENUMERAL_TYPE)
    1210          898 :         srctype = bitwise_type_for_mode (TYPE_MODE (srctype));
    1211       319286 :       if (!srctype)
    1212          120 :         srctype = desttype;
    1213       319286 :       if (!desttype)
    1214            0 :         desttype = srctype;
    1215       319286 :       if (!srctype)
    1216              :         return false;
    1217              : 
    1218       319286 :       src_align = get_pointer_alignment (src);
    1219       319286 :       dest_align = get_pointer_alignment (dest);
    1220              : 
    1221              :       /* Choose between src and destination type for the access based
    1222              :          on alignment, whether the access constitutes a register access
    1223              :          and whether it may actually expose a declaration for SSA rewrite
    1224              :          or SRA decomposition.  Also try to expose a string constant, we
    1225              :          might be able to concatenate several of them later into a single
    1226              :          string store.  */
    1227       319286 :       destvar = NULL_TREE;
    1228       319286 :       srcvar = NULL_TREE;
    1229       319286 :       if (TREE_CODE (dest) == ADDR_EXPR
    1230       107446 :           && var_decl_component_p (TREE_OPERAND (dest, 0))
    1231       107442 :           && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len)
    1232        25491 :           && dest_align >= TYPE_ALIGN (desttype)
    1233       344777 :           && (is_gimple_reg_type (desttype)
    1234        25063 :               || src_align >= TYPE_ALIGN (desttype)))
    1235        20119 :         destvar = fold_build2 (MEM_REF, desttype, dest, off0);
    1236       299167 :       else if (TREE_CODE (src) == ADDR_EXPR
    1237       233353 :                && var_decl_component_p (TREE_OPERAND (src, 0))
    1238        47930 :                && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len)
    1239         9241 :                && src_align >= TYPE_ALIGN (srctype)
    1240       308392 :                && (is_gimple_reg_type (srctype)
    1241         9062 :                    || dest_align >= TYPE_ALIGN (srctype)))
    1242         3368 :         srcvar = fold_build2 (MEM_REF, srctype, src, off0);
    1243              :       /* FIXME: Don't transform copies from strings with known original length.
    1244              :          As soon as strlenopt tests that rely on it for passing are adjusted,
    1245              :          this hack can be removed.  */
    1246       295799 :       else if (gimple_call_alloca_for_var_p (stmt)
    1247          115 :                && (srcvar = string_constant (src, &srcoff, NULL, NULL))
    1248            3 :                && integer_zerop (srcoff)
    1249            3 :                && tree_int_cst_equal (TYPE_SIZE_UNIT (TREE_TYPE (srcvar)), len)
    1250       295802 :                && dest_align >= TYPE_ALIGN (TREE_TYPE (srcvar)))
    1251            3 :         srctype = TREE_TYPE (srcvar);
    1252              :       else
    1253              :         return false;
    1254              : 
    1255              :       /* Now that we chose an access type express the other side in
    1256              :          terms of it if the target allows that with respect to alignment
    1257              :          constraints.  */
    1258        23490 :       if (srcvar == NULL_TREE)
    1259              :         {
    1260        20119 :           if (src_align >= TYPE_ALIGN (desttype))
    1261        20101 :             srcvar = fold_build2 (MEM_REF, desttype, src, off0);
    1262              :           else
    1263              :             {
    1264           18 :               enum machine_mode mode = TYPE_MODE (desttype);
    1265           18 :               if ((mode == BLKmode && STRICT_ALIGNMENT)
    1266           18 :                   || (targetm.slow_unaligned_access (mode, src_align)
    1267           18 :                       && (optab_handler (movmisalign_optab, mode)
    1268              :                           == CODE_FOR_nothing)))
    1269              :                 return false;
    1270           18 :               srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
    1271              :                                             src_align);
    1272           18 :               srcvar = fold_build2 (MEM_REF, srctype, src, off0);
    1273              :             }
    1274              :         }
    1275         3371 :       else if (destvar == NULL_TREE)
    1276              :         {
    1277         3371 :           if (dest_align >= TYPE_ALIGN (srctype))
    1278         3371 :             destvar = fold_build2 (MEM_REF, srctype, dest, off0);
    1279              :           else
    1280              :             {
    1281            0 :               enum machine_mode mode = TYPE_MODE (srctype);
    1282            0 :               if ((mode == BLKmode && STRICT_ALIGNMENT)
    1283            0 :                   || (targetm.slow_unaligned_access (mode, dest_align)
    1284            0 :                       && (optab_handler (movmisalign_optab, mode)
    1285              :                           == CODE_FOR_nothing)))
    1286              :                 return false;
    1287            0 :               desttype = build_aligned_type (TYPE_MAIN_VARIANT (srctype),
    1288              :                                              dest_align);
    1289            0 :               destvar = fold_build2 (MEM_REF, desttype, dest, off0);
    1290              :             }
    1291              :         }
    1292              : 
    1293              :       /* Same as above, detect out-of-bounds accesses without issuing
    1294              :          warnings.  Avoid folding out-of-bounds copies but to avoid
    1295              :          false positives for unreachable code defer warning until
    1296              :          after DCE has worked its magic.
    1297              :          -Wrestrict is still diagnosed.  */
    1298        23490 :       if (int warning = check_bounds_or_overlap (as_a <gcall *>(stmt),
    1299              :                                                  dest, src, len, len,
    1300        23490 :                                                  false, false))
    1301         1263 :         if (warning != OPT_Wrestrict)
    1302              :           return false;
    1303              : 
    1304        22235 :       gassign *new_stmt;
    1305        22235 :       if (is_gimple_reg_type (TREE_TYPE (srcvar)))
    1306              :         {
    1307          551 :           tree tem = fold_const_aggregate_ref (srcvar);
    1308          551 :           if (tem)
    1309          530 :             srcvar = tem;
    1310          551 :           if (! is_gimple_min_invariant (srcvar))
    1311              :             {
    1312           21 :               new_stmt = gimple_build_assign (NULL_TREE, srcvar);
    1313           21 :               srcvar = make_ssa_name (TREE_TYPE (srcvar), new_stmt);
    1314           21 :               gimple_assign_set_lhs (new_stmt, srcvar);
    1315           42 :               gimple_set_vuse (new_stmt, gimple_vuse (stmt));
    1316           21 :               gimple_set_location (new_stmt, loc);
    1317           21 :               gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
    1318              :             }
    1319          551 :           new_stmt = gimple_build_assign (destvar, srcvar);
    1320          551 :           goto set_vop_and_replace;
    1321              :         }
    1322              : 
    1323              :       /* We get an aggregate copy.  If the source is a STRING_CST, then
    1324              :          directly use its type to perform the copy.  */
    1325        21684 :       if (TREE_CODE (srcvar) == STRING_CST)
    1326              :           desttype = srctype;
    1327              : 
    1328              :       /* Or else, use an unsigned char[] type to perform the copy in order
    1329              :          to preserve padding and to avoid any issues with TREE_ADDRESSABLE
    1330              :          types or float modes behavior on copying.  */
    1331              :       else
    1332              :         {
    1333        43362 :           desttype = build_array_type_nelts (unsigned_char_type_node,
    1334        21681 :                                              tree_to_uhwi (len));
    1335        21681 :           srctype = desttype;
    1336        21681 :           if (src_align > TYPE_ALIGN (srctype))
    1337        13090 :             srctype = build_aligned_type (srctype, src_align);
    1338        21681 :           srcvar = fold_build2 (MEM_REF, srctype, src, off0);
    1339              :         }
    1340              : 
    1341        21684 :       if (dest_align > TYPE_ALIGN (desttype))
    1342        13716 :         desttype = build_aligned_type (desttype, dest_align);
    1343        21684 :       destvar = fold_build2 (MEM_REF, desttype, dest, off0);
    1344        21684 :       new_stmt = gimple_build_assign (destvar, srcvar);
    1345              : 
    1346        22235 : set_vop_and_replace:
    1347        22235 :       gimple_move_vops (new_stmt, stmt);
    1348        22235 :       if (!lhs)
    1349              :         {
    1350        22051 :           gsi_replace (gsi, new_stmt, false);
    1351        22051 :           return true;
    1352              :         }
    1353          184 :       gimple_set_location (new_stmt, loc);
    1354          184 :       gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
    1355              :     }
    1356              : 
    1357         3568 : done:
    1358         3568 :   gimple_seq stmts = NULL;
    1359         3568 :   if (code == BUILT_IN_MEMCPY || code == BUILT_IN_MEMMOVE)
    1360         3568 :     len = NULL_TREE;
    1361          200 :   else if (code == BUILT_IN_MEMPCPY)
    1362              :     {
    1363          200 :       len = gimple_convert_to_ptrofftype (&stmts, loc, len);
    1364          200 :       dest = gimple_build (&stmts, loc, POINTER_PLUS_EXPR,
    1365          200 :                            TREE_TYPE (dest), dest, len);
    1366              :     }
    1367              :   else
    1368            0 :     gcc_unreachable ();
    1369              : 
    1370         3568 :   gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
    1371         3568 :   gimple *repl = gimple_build_assign (lhs, dest);
    1372         3568 :   gsi_replace (gsi, repl, false);
    1373         3568 :   return true;
    1374              : }
    1375              : 
    1376              : /* Transform a call to built-in bcmp(a, b, len) at *GSI into one
    1377              :    to built-in memcmp (a, b, len).  */
    1378              : 
    1379              : static bool
    1380          149 : gimple_fold_builtin_bcmp (gimple_stmt_iterator *gsi)
    1381              : {
    1382          149 :   tree fn = builtin_decl_implicit (BUILT_IN_MEMCMP);
    1383              : 
    1384          149 :   if (!fn)
    1385              :     return false;
    1386              : 
    1387              :   /* Transform bcmp (a, b, len) into memcmp (a, b, len).  */
    1388              : 
    1389          149 :   gimple *stmt = gsi_stmt (*gsi);
    1390          298 :   if (!gimple_vuse (stmt) && gimple_in_ssa_p (cfun))
    1391              :     return false;
    1392          149 :   tree a = gimple_call_arg (stmt, 0);
    1393          149 :   tree b = gimple_call_arg (stmt, 1);
    1394          149 :   tree len = gimple_call_arg (stmt, 2);
    1395              : 
    1396          149 :   gimple *repl = gimple_build_call (fn, 3, a, b, len);
    1397          149 :   replace_call_with_call_and_fold (gsi, repl);
    1398              : 
    1399          149 :   return true;
    1400              : }
    1401              : 
    1402              : /* Transform a call to built-in bcopy (src, dest, len) at *GSI into one
    1403              :    to built-in memmove (dest, src, len).  */
    1404              : 
    1405              : static bool
    1406          367 : gimple_fold_builtin_bcopy (gimple_stmt_iterator *gsi)
    1407              : {
    1408          367 :   tree fn = builtin_decl_implicit (BUILT_IN_MEMMOVE);
    1409              : 
    1410          367 :   if (!fn)
    1411              :     return false;
    1412              : 
    1413              :   /* bcopy has been removed from POSIX in Issue 7 but Issue 6 specifies
    1414              :      it's equivalent to memmove (not memcpy).  Transform bcopy (src, dest,
    1415              :      len) into memmove (dest, src, len).  */
    1416              : 
    1417          367 :   gimple *stmt = gsi_stmt (*gsi);
    1418          734 :   if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
    1419              :     return false;
    1420          367 :   tree src = gimple_call_arg (stmt, 0);
    1421          367 :   tree dest = gimple_call_arg (stmt, 1);
    1422          367 :   tree len = gimple_call_arg (stmt, 2);
    1423              : 
    1424          367 :   gimple *repl = gimple_build_call (fn, 3, dest, src, len);
    1425          367 :   gimple_call_set_fntype (as_a <gcall *> (stmt), TREE_TYPE (fn));
    1426          367 :   replace_call_with_call_and_fold (gsi, repl);
    1427              : 
    1428          367 :   return true;
    1429              : }
    1430              : 
    1431              : /* Transform a call to built-in bzero (dest, len) at *GSI into one
    1432              :    to built-in memset (dest, 0, len).  */
    1433              : 
    1434              : static bool
    1435          250 : gimple_fold_builtin_bzero (gimple_stmt_iterator *gsi)
    1436              : {
    1437          250 :   tree fn = builtin_decl_implicit (BUILT_IN_MEMSET);
    1438              : 
    1439          250 :   if (!fn)
    1440              :     return false;
    1441              : 
    1442              :   /* Transform bzero (dest, len) into memset (dest, 0, len).  */
    1443              : 
    1444          250 :   gimple *stmt = gsi_stmt (*gsi);
    1445          500 :   if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
    1446              :     return false;
    1447          250 :   tree dest = gimple_call_arg (stmt, 0);
    1448          250 :   tree len = gimple_call_arg (stmt, 1);
    1449              : 
    1450          250 :   gimple_seq seq = NULL;
    1451          250 :   gimple *repl = gimple_build_call (fn, 3, dest, integer_zero_node, len);
    1452          250 :   gimple_seq_add_stmt_without_update (&seq, repl);
    1453          250 :   gsi_replace_with_seq_vops (gsi, seq);
    1454          250 :   fold_stmt (gsi);
    1455              : 
    1456          250 :   return true;
    1457              : }
    1458              : 
    1459              : /* Fold function call to builtin memset or bzero at *GSI setting the
    1460              :    memory of size LEN to VAL.  Return whether a simplification was made.  */
    1461              : 
    1462              : static bool
    1463       329106 : gimple_fold_builtin_memset (gimple_stmt_iterator *gsi, tree c, tree len)
    1464              : {
    1465       329106 :   gimple *stmt = gsi_stmt (*gsi);
    1466       329106 :   tree etype;
    1467       329106 :   unsigned HOST_WIDE_INT length, cval;
    1468              : 
    1469              :   /* If the LEN parameter is zero, return DEST.  */
    1470       329106 :   if (integer_zerop (len))
    1471              :     {
    1472          835 :       replace_call_with_value (gsi, gimple_call_arg (stmt, 0));
    1473          835 :       return true;
    1474              :     }
    1475              : 
    1476       656542 :   if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
    1477              :     return false;
    1478              : 
    1479       328193 :   if (! tree_fits_uhwi_p (len))
    1480              :     return false;
    1481              : 
    1482       217982 :   length = tree_to_uhwi (len);
    1483              : 
    1484       217982 :   tree dest = gimple_call_arg (stmt, 0);
    1485       217982 :   if (length == 1
    1486       217982 :       && POINTER_TYPE_P (TREE_TYPE (dest)))
    1487              :     {
    1488              :       /* Keep the original call until object-size analysis has inspected it.  */
    1489         3526 :       if (!(cfun->curr_properties & PROP_objsz))
    1490              :         return false;
    1491              : 
    1492              :       /* Detect out-of-bounds accesses without issuing warnings.
    1493              :          Avoid folding out-of-bounds accesses but to avoid false
    1494              :          positives for unreachable code defer warning until after
    1495              :          DCE has worked its magic.
    1496              :          -Wrestrict is still diagnosed.  */
    1497          605 :       if (int warning = check_bounds_or_overlap (as_a <gcall *>(stmt),
    1498              :                                                  dest, NULL_TREE, len,
    1499          605 :                                                  NULL_TREE, false, false))
    1500           20 :         if (warning != OPT_Wrestrict)
    1501              :           return false;
    1502              : 
    1503          585 :       etype = unsigned_char_type_node;
    1504          585 :       tree ptype = TREE_TYPE (TREE_TYPE (dest));
    1505          585 :       if (TYPE_VOLATILE (ptype))
    1506            0 :         etype = build_qualified_type (etype, TYPE_QUAL_VOLATILE);
    1507              : 
    1508          585 :       location_t loc = gimple_location (stmt);
    1509          585 :       tree cval_tree;
    1510          585 :       if (TREE_CODE (c) == INTEGER_CST)
    1511          540 :         cval_tree = fold_convert (etype, c);
    1512              :       else
    1513           45 :         cval_tree = gimple_convert (gsi, true, GSI_SAME_STMT, loc, etype, c);
    1514              : 
    1515              :       /* Build accesses at offset zero with a ref-all character type.  */
    1516          585 :       tree off0
    1517          585 :         = build_int_cst (build_pointer_type_for_mode (char_type_node,
    1518              :                                                       ptr_mode, true), 0);
    1519          585 :       tree var = fold_build2_loc (loc, MEM_REF, etype, dest, off0);
    1520          585 :       gimple *store = gimple_build_assign (var, cval_tree);
    1521          585 :       gimple_move_vops (store, stmt);
    1522          585 :       gimple_set_location (store, loc);
    1523          585 :       copy_warning (store, stmt);
    1524              : 
    1525          585 :       tree lhs = gimple_call_lhs (stmt);
    1526          585 :       if (!lhs)
    1527              :         {
    1528          476 :           gsi_replace (gsi, store, false);
    1529          476 :           return true;
    1530              :         }
    1531              : 
    1532          109 :       gsi_insert_before (gsi, store, GSI_SAME_STMT);
    1533          109 :       tree ret = dest;
    1534          109 :       if (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (dest)))
    1535            0 :         ret = gimple_convert (gsi, true, GSI_SAME_STMT, loc,
    1536            0 :                               TREE_TYPE (lhs), dest);
    1537          109 :       gimple *asgn = gimple_build_assign (lhs, ret);
    1538          109 :       gsi_replace (gsi, asgn, false);
    1539              : 
    1540          109 :       return true;
    1541              :     }
    1542              : 
    1543       214456 :   if (TREE_CODE (c) != INTEGER_CST)
    1544              :     return false;
    1545              : 
    1546       208752 :   tree var = dest;
    1547       208752 :   if (TREE_CODE (var) != ADDR_EXPR)
    1548              :     return false;
    1549              : 
    1550       169679 :   var = TREE_OPERAND (var, 0);
    1551       169679 :   if (TREE_THIS_VOLATILE (var))
    1552              :     return false;
    1553              : 
    1554       169636 :   etype = TREE_TYPE (var);
    1555       169636 :   if (TREE_CODE (etype) == ARRAY_TYPE)
    1556        92968 :     etype = TREE_TYPE (etype);
    1557              : 
    1558       169636 :   if ((!INTEGRAL_TYPE_P (etype)
    1559        96705 :        && !POINTER_TYPE_P (etype))
    1560       170080 :       || BITINT_TYPE_P (etype))
    1561              :     return false;
    1562              : 
    1563        72024 :   if (! var_decl_component_p (var))
    1564              :     return false;
    1565              : 
    1566        72024 :   if (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (etype)) != length
    1567         1450 :       || (GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (etype))
    1568        73474 :           != GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (etype)))
    1569        73474 :       || get_pointer_alignment (dest) / BITS_PER_UNIT < length)
    1570              :     return false;
    1571              : 
    1572         1450 :   if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
    1573              :     return false;
    1574              : 
    1575         1450 :   if (!type_has_mode_precision_p (etype))
    1576            7 :     etype = lang_hooks.types.type_for_mode (SCALAR_INT_TYPE_MODE (etype),
    1577            7 :                                             TYPE_UNSIGNED (etype));
    1578              : 
    1579         1450 :   if (integer_zerop (c))
    1580              :     cval = 0;
    1581              :   else
    1582              :     {
    1583           78 :       if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
    1584              :         return NULL_TREE;
    1585              : 
    1586           78 :       cval = TREE_INT_CST_LOW (c);
    1587           78 :       cval &= 0xff;
    1588           78 :       cval |= cval << 8;
    1589           78 :       cval |= cval << 16;
    1590           78 :       cval |= (cval << 31) << 1;
    1591              :     }
    1592              : 
    1593         1450 :   var = fold_build2 (MEM_REF, etype, dest, build_int_cst (ptr_type_node, 0));
    1594         1450 :   gimple *store = gimple_build_assign (var, build_int_cst_type (etype, cval));
    1595         1450 :   gimple_move_vops (store, stmt);
    1596         1450 :   gimple_set_location (store, gimple_location (stmt));
    1597         1450 :   gsi_insert_before (gsi, store, GSI_SAME_STMT);
    1598         1450 :   if (gimple_call_lhs (stmt))
    1599              :     {
    1600            0 :       gimple *asgn = gimple_build_assign (gimple_call_lhs (stmt), dest);
    1601            0 :       gsi_replace (gsi, asgn, false);
    1602              :     }
    1603              :   else
    1604              :     {
    1605         1450 :       gimple_stmt_iterator gsi2 = *gsi;
    1606         1450 :       gsi_prev (gsi);
    1607         1450 :       gsi_remove (&gsi2, true);
    1608              :     }
    1609              : 
    1610              :   return true;
    1611              : }
    1612              : 
    1613              : /* Helper of get_range_strlen for ARG that is not an SSA_NAME.  */
    1614              : 
    1615              : static bool
    1616       448928 : get_range_strlen_tree (tree arg, bitmap visited, strlen_range_kind rkind,
    1617              :                        c_strlen_data *pdata, unsigned eltsize)
    1618              : {
    1619       448928 :   gcc_assert (TREE_CODE (arg) != SSA_NAME);
    1620              : 
    1621              :   /* The length computed by this invocation of the function.  */
    1622       448928 :   tree val = NULL_TREE;
    1623              : 
    1624              :   /* True if VAL is an optimistic (tight) bound determined from
    1625              :      the size of the character array in which the string may be
    1626              :      stored.  In that case, the computed VAL is used to set
    1627              :      PDATA->MAXBOUND.  */
    1628       448928 :   bool tight_bound = false;
    1629              : 
    1630              :   /* We can end up with &(*iftmp_1)[0] here as well, so handle it.  */
    1631       448928 :   if (TREE_CODE (arg) == ADDR_EXPR
    1632       448928 :       && TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF)
    1633              :     {
    1634        28830 :       tree op = TREE_OPERAND (arg, 0);
    1635        28830 :       if (integer_zerop (TREE_OPERAND (op, 1)))
    1636              :         {
    1637        12576 :           tree aop0 = TREE_OPERAND (op, 0);
    1638        12576 :           if (TREE_CODE (aop0) == INDIRECT_REF
    1639        12576 :               && TREE_CODE (TREE_OPERAND (aop0, 0)) == SSA_NAME)
    1640            0 :             return get_range_strlen (TREE_OPERAND (aop0, 0), visited, rkind,
    1641            0 :                                      pdata, eltsize);
    1642              :         }
    1643        16254 :       else if (TREE_CODE (TREE_OPERAND (op, 0)) == COMPONENT_REF
    1644        16254 :                && rkind == SRK_LENRANGE)
    1645              :         {
    1646              :           /* Fail if an array is the last member of a struct object
    1647              :              since it could be treated as a (fake) flexible array
    1648              :              member.  */
    1649         4800 :           tree idx = TREE_OPERAND (op, 1);
    1650              : 
    1651         4800 :           arg = TREE_OPERAND (op, 0);
    1652         4800 :           tree optype = TREE_TYPE (arg);
    1653         4800 :           if (tree dom = TYPE_DOMAIN (optype))
    1654         4800 :             if (tree bound = TYPE_MAX_VALUE (dom))
    1655         4800 :               if (TREE_CODE (bound) == INTEGER_CST
    1656         4800 :                   && TREE_CODE (idx) == INTEGER_CST
    1657         8030 :                   && tree_int_cst_lt (bound, idx))
    1658              :                 return false;
    1659              :         }
    1660              :     }
    1661              : 
    1662       448720 :   if (rkind == SRK_INT_VALUE)
    1663              :     {
    1664              :       /* We are computing the maximum value (not string length).  */
    1665         3083 :       val = arg;
    1666         3083 :       if (TREE_CODE (val) != INTEGER_CST
    1667         3083 :           || tree_int_cst_sgn (val) < 0)
    1668              :         return false;
    1669              :     }
    1670              :   else
    1671              :     {
    1672       445637 :       c_strlen_data lendata = { };
    1673       445637 :       val = c_strlen (arg, 1, &lendata, eltsize);
    1674              : 
    1675       445637 :       if (!val && lendata.decl)
    1676              :         {
    1677              :           /* ARG refers to an unterminated const character array.
    1678              :              DATA.DECL with size DATA.LEN.  */
    1679         4226 :           val = lendata.minlen;
    1680         4226 :           pdata->decl = lendata.decl;
    1681              :         }
    1682              :     }
    1683              : 
    1684              :   /* Set if VAL represents the maximum length based on array size (set
    1685              :      when exact length cannot be determined).  */
    1686       446147 :   bool maxbound = false;
    1687              : 
    1688       446147 :   if (!val && rkind == SRK_LENRANGE)
    1689              :     {
    1690       244196 :       if (TREE_CODE (arg) == ADDR_EXPR)
    1691        87396 :         return get_range_strlen (TREE_OPERAND (arg, 0), visited, rkind,
    1692        87396 :                                  pdata, eltsize);
    1693              : 
    1694       156800 :       if (TREE_CODE (arg) == ARRAY_REF)
    1695              :         {
    1696        17985 :           tree optype = TREE_TYPE (TREE_OPERAND (arg, 0));
    1697              : 
    1698              :           /* Determine the "innermost" array type.  */
    1699        17985 :           while (TREE_CODE (optype) == ARRAY_TYPE
    1700        24942 :                  && TREE_CODE (TREE_TYPE (optype)) == ARRAY_TYPE)
    1701         6957 :             optype = TREE_TYPE (optype);
    1702              : 
    1703              :           /* Avoid arrays of pointers.  */
    1704        17985 :           tree eltype = TREE_TYPE (optype);
    1705        17985 :           if (TREE_CODE (optype) != ARRAY_TYPE
    1706        17985 :               || !INTEGRAL_TYPE_P (eltype))
    1707              :             return false;
    1708              : 
    1709              :           /* Fail when the array bound is unknown or zero.  */
    1710        13683 :           val = TYPE_SIZE_UNIT (optype);
    1711        13683 :           if (!val
    1712        13611 :               || TREE_CODE (val) != INTEGER_CST
    1713        27266 :               || integer_zerop (val))
    1714              :             return false;
    1715              : 
    1716        13578 :           val = fold_build2 (MINUS_EXPR, TREE_TYPE (val), val,
    1717              :                               integer_one_node);
    1718              : 
    1719              :           /* Set the minimum size to zero since the string in
    1720              :              the array could have zero length.  */
    1721        13578 :           pdata->minlen = ssize_int (0);
    1722              : 
    1723        13578 :           tight_bound = true;
    1724              :         }
    1725       138815 :       else if (TREE_CODE (arg) == COMPONENT_REF
    1726       138815 :                && (TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 1)))
    1727              :                    == ARRAY_TYPE))
    1728              :         {
    1729              :           /* Use the type of the member array to determine the upper
    1730              :              bound on the length of the array.  This may be overly
    1731              :              optimistic if the array itself isn't NUL-terminated and
    1732              :              the caller relies on the subsequent member to contain
    1733              :              the NUL but that would only be considered valid if
    1734              :              the array were the last member of a struct.  */
    1735              : 
    1736        10063 :           tree fld = TREE_OPERAND (arg, 1);
    1737              : 
    1738        10063 :           tree optype = TREE_TYPE (fld);
    1739              : 
    1740              :           /* Determine the "innermost" array type.  */
    1741        10063 :           while (TREE_CODE (optype) == ARRAY_TYPE
    1742        10625 :                  && TREE_CODE (TREE_TYPE (optype)) == ARRAY_TYPE)
    1743          562 :             optype = TREE_TYPE (optype);
    1744              : 
    1745              :           /* Fail when the array bound is unknown or zero.  */
    1746        10063 :           val = TYPE_SIZE_UNIT (optype);
    1747        10063 :           if (!val
    1748         9827 :               || TREE_CODE (val) != INTEGER_CST
    1749        19855 :               || integer_zerop (val))
    1750              :             return false;
    1751         9713 :           val = fold_build2 (MINUS_EXPR, TREE_TYPE (val), val,
    1752              :                              integer_one_node);
    1753              : 
    1754              :           /* Set the minimum size to zero since the string in
    1755              :              the array could have zero length.  */
    1756         9713 :           pdata->minlen = ssize_int (0);
    1757              : 
    1758              :           /* The array size determined above is an optimistic bound
    1759              :              on the length.  If the array isn't nul-terminated the
    1760              :              length computed by the library function would be greater.
    1761              :              Even though using strlen to cross the subobject boundary
    1762              :              is undefined, avoid drawing conclusions from the member
    1763              :              type about the length here.  */
    1764         9713 :           tight_bound = true;
    1765              :         }
    1766       128752 :       else if (TREE_CODE (arg) == MEM_REF
    1767        34627 :                && TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
    1768        10269 :                && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == INTEGER_TYPE
    1769       138583 :                && TREE_CODE (TREE_OPERAND (arg, 0)) == ADDR_EXPR)
    1770              :         {
    1771              :           /* Handle a MEM_REF into a DECL accessing an array of integers,
    1772              :              being conservative about references to extern structures with
    1773              :              flexible array members that can be initialized to arbitrary
    1774              :              numbers of elements as an extension (static structs are okay).  */
    1775         9831 :           tree ref = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
    1776         9831 :           if ((TREE_CODE (ref) == PARM_DECL || VAR_P (ref))
    1777        19649 :               && (decl_binds_to_current_def_p (ref)
    1778          438 :                   || !array_ref_flexible_size_p (arg)))
    1779              :             {
    1780              :               /* Fail if the offset is out of bounds.  Such accesses
    1781              :                  should be diagnosed at some point.  */
    1782         9705 :               val = DECL_SIZE_UNIT (ref);
    1783         9705 :               if (!val
    1784         9533 :                   || TREE_CODE (val) != INTEGER_CST
    1785        19238 :                   || integer_zerop (val))
    1786          371 :                 return false;
    1787              : 
    1788         9531 :               poly_offset_int psiz = wi::to_offset (val);
    1789         9531 :               poly_offset_int poff = mem_ref_offset (arg);
    1790         9531 :               if (known_le (psiz, poff))
    1791              :                 return false;
    1792              : 
    1793         9334 :               pdata->minlen = ssize_int (0);
    1794              : 
    1795              :               /* Subtract the offset and one for the terminating nul.  */
    1796         9334 :               psiz -= poff;
    1797         9334 :               psiz -= 1;
    1798         9334 :               val = wide_int_to_tree (TREE_TYPE (val), psiz);
    1799              :               /* Since VAL reflects the size of a declared object
    1800              :                  rather the type of the access it is not a tight bound.  */
    1801              :             }
    1802              :         }
    1803       118921 :       else if (TREE_CODE (arg) == PARM_DECL || VAR_P (arg))
    1804              :         {
    1805              :           /* Avoid handling pointers to arrays.  GCC might misuse
    1806              :              a pointer to an array of one bound to point to an array
    1807              :              object of a greater bound.  */
    1808        71080 :           tree argtype = TREE_TYPE (arg);
    1809        71080 :           if (TREE_CODE (argtype) == ARRAY_TYPE)
    1810              :             {
    1811        42705 :               val = TYPE_SIZE_UNIT (argtype);
    1812        42705 :               if (!val
    1813        41939 :                   || TREE_CODE (val) != INTEGER_CST
    1814        84644 :                   || integer_zerop (val))
    1815              :                 return false;
    1816        41824 :               val = wide_int_to_tree (TREE_TYPE (val),
    1817        41824 :                                       wi::sub (wi::to_wide (val), 1));
    1818              : 
    1819              :               /* Set the minimum size to zero since the string in
    1820              :                  the array could have zero length.  */
    1821        41824 :               pdata->minlen = ssize_int (0);
    1822              :             }
    1823              :         }
    1824              :       maxbound = true;
    1825              :     }
    1826              : 
    1827       352742 :   if (!val)
    1828              :     return false;
    1829              : 
    1830              :   /* Adjust the lower bound on the string length as necessary.  */
    1831       251827 :   if (!pdata->minlen
    1832       251827 :       || (rkind != SRK_STRLEN
    1833        78883 :           && TREE_CODE (pdata->minlen) == INTEGER_CST
    1834        78883 :           && TREE_CODE (val) == INTEGER_CST
    1835        78878 :           && tree_int_cst_lt (val, pdata->minlen)))
    1836       172963 :     pdata->minlen = val;
    1837              : 
    1838       251827 :   if (pdata->maxbound && TREE_CODE (pdata->maxbound) == INTEGER_CST)
    1839              :     {
    1840              :       /* Adjust the tighter (more optimistic) string length bound
    1841              :          if necessary and proceed to adjust the more conservative
    1842              :          bound.  */
    1843         7715 :       if (TREE_CODE (val) == INTEGER_CST)
    1844              :         {
    1845         7715 :           if (tree_int_cst_lt (pdata->maxbound, val))
    1846          678 :             pdata->maxbound = val;
    1847              :         }
    1848              :       else
    1849            0 :         pdata->maxbound = val;
    1850              :     }
    1851       244112 :   else if (pdata->maxbound || maxbound)
    1852              :     /* Set PDATA->MAXBOUND only if it either isn't INTEGER_CST or
    1853              :        if VAL corresponds to the maximum length determined based
    1854              :        on the type of the object.  */
    1855        71157 :     pdata->maxbound = val;
    1856              : 
    1857       251827 :   if (tight_bound)
    1858              :     {
    1859              :       /* VAL computed above represents an optimistically tight bound
    1860              :          on the length of the string based on the referenced object's
    1861              :          or subobject's type.  Determine the conservative upper bound
    1862              :          based on the enclosing object's size if possible.  */
    1863        23291 :       if (rkind == SRK_LENRANGE)
    1864              :         {
    1865        23291 :           poly_int64 offset;
    1866        23291 :           tree base = get_addr_base_and_unit_offset (arg, &offset);
    1867        23291 :           if (!base)
    1868              :             {
    1869              :               /* When the call above fails due to a non-constant offset
    1870              :                  assume the offset is zero and use the size of the whole
    1871              :                  enclosing object instead.  */
    1872         7846 :               base = get_base_address (arg);
    1873         7846 :               offset = 0;
    1874              :             }
    1875              :           /* If the base object is a pointer no upper bound on the length
    1876              :              can be determined.  Otherwise the maximum length is equal to
    1877              :              the size of the enclosing object minus the offset of
    1878              :              the referenced subobject minus 1 (for the terminating nul).  */
    1879        23291 :           tree type = TREE_TYPE (base);
    1880        23291 :           if (POINTER_TYPE_P (type)
    1881        23287 :               || (TREE_CODE (base) != PARM_DECL && !VAR_P (base))
    1882        42007 :               || !(val = DECL_SIZE_UNIT (base)))
    1883         5822 :             val = build_all_ones_cst (size_type_node);
    1884              :           else
    1885              :             {
    1886        17469 :               val = DECL_SIZE_UNIT (base);
    1887        17469 :               val = fold_build2 (MINUS_EXPR, TREE_TYPE (val), val,
    1888              :                                  size_int (offset + 1));
    1889              :             }
    1890              :         }
    1891              :       else
    1892              :         return false;
    1893              :     }
    1894              : 
    1895       251827 :   if (pdata->maxlen)
    1896              :     {
    1897              :       /* Adjust the more conservative bound if possible/necessary
    1898              :          and fail otherwise.  */
    1899        14863 :       if (rkind != SRK_STRLEN)
    1900              :         {
    1901        13932 :           if (TREE_CODE (pdata->maxlen) != INTEGER_CST
    1902        13932 :               || TREE_CODE (val) != INTEGER_CST)
    1903              :             return false;
    1904              : 
    1905        13927 :           if (tree_int_cst_lt (pdata->maxlen, val))
    1906         1381 :             pdata->maxlen = val;
    1907              :           return true;
    1908              :         }
    1909          931 :       else if (simple_cst_equal (val, pdata->maxlen) != 1)
    1910              :         {
    1911              :           /* Fail if the length of this ARG is different from that
    1912              :              previously determined from another ARG.  */
    1913              :           return false;
    1914              :         }
    1915              :     }
    1916              : 
    1917       237088 :   pdata->maxlen = val;
    1918       237088 :   return rkind == SRK_LENRANGE || !integer_all_onesp (val);
    1919              : }
    1920              : 
    1921              : /* For an ARG referencing one or more strings, try to obtain the range
    1922              :    of their lengths, or the size of the largest array ARG refers to if
    1923              :    the range of lengths cannot be determined, and store all in *PDATA.
    1924              :    For an integer ARG (when RKIND == SRK_INT_VALUE), try to determine
    1925              :    the maximum constant value.
    1926              :    If ARG is an SSA_NAME, follow its use-def chains.  When RKIND ==
    1927              :    SRK_STRLEN, then if PDATA->MAXLEN is not equal to the determined
    1928              :    length or if we are unable to determine the length, return false.
    1929              :    VISITED is a bitmap of visited variables.
    1930              :    RKIND determines the kind of value or range to obtain (see
    1931              :    strlen_range_kind).
    1932              :    Set PDATA->DECL if ARG refers to an unterminated constant array.
    1933              :    On input, set ELTSIZE to 1 for normal single byte character strings,
    1934              :    and either 2 or 4 for wide character strings (the size of wchar_t).
    1935              :    Return true if *PDATA was successfully populated and false otherwise.  */
    1936              : 
    1937              : static bool
    1938      1358114 : get_range_strlen (tree arg, bitmap visited,
    1939              :                   strlen_range_kind rkind,
    1940              :                   c_strlen_data *pdata, unsigned eltsize)
    1941              : {
    1942              : 
    1943      1439334 :   if (TREE_CODE (arg) != SSA_NAME)
    1944       448928 :     return get_range_strlen_tree (arg, visited, rkind, pdata, eltsize);
    1945              : 
    1946              :   /* If ARG is registered for SSA update we cannot look at its defining
    1947              :      statement.  */
    1948       990406 :   if (name_registered_for_update_p (arg))
    1949              :     return false;
    1950              : 
    1951              :   /* If we were already here, break the infinite cycle.  */
    1952       990406 :   if (!bitmap_set_bit (visited, SSA_NAME_VERSION (arg)))
    1953              :     return true;
    1954              : 
    1955       984657 :   tree var = arg;
    1956       984657 :   gimple *def_stmt = SSA_NAME_DEF_STMT (var);
    1957              : 
    1958       984657 :   switch (gimple_code (def_stmt))
    1959              :     {
    1960       124340 :       case GIMPLE_ASSIGN:
    1961              :         /* The RHS of the statement defining VAR must either have a
    1962              :            constant length or come from another SSA_NAME with a constant
    1963              :            length.  */
    1964       124340 :         if (gimple_assign_single_p (def_stmt)
    1965       124340 :             || gimple_assign_unary_nop_p (def_stmt))
    1966              :           {
    1967        81220 :             tree rhs = gimple_assign_rhs1 (def_stmt);
    1968        81220 :             return get_range_strlen (rhs, visited, rkind, pdata, eltsize);
    1969              :           }
    1970        43120 :         else if (gimple_assign_rhs_code (def_stmt) == COND_EXPR)
    1971              :           {
    1972          246 :             tree ops[2] = { gimple_assign_rhs2 (def_stmt),
    1973          246 :                             gimple_assign_rhs3 (def_stmt) };
    1974              : 
    1975          738 :             for (unsigned int i = 0; i < 2; i++)
    1976          492 :               if (!get_range_strlen (ops[i], visited, rkind, pdata, eltsize))
    1977              :                 {
    1978           28 :                   if (rkind != SRK_LENRANGE)
    1979              :                     return false;
    1980              :                   /* Set the upper bound to the maximum to prevent
    1981              :                      it from being adjusted in the next iteration but
    1982              :                      leave MINLEN and the more conservative MAXBOUND
    1983              :                      determined so far alone (or leave them null if
    1984              :                      they haven't been set yet).  That the MINLEN is
    1985              :                      in fact zero can be determined from MAXLEN being
    1986              :                      unbounded but the discovered minimum is used for
    1987              :                      diagnostics.  */
    1988           28 :                   pdata->maxlen = build_all_ones_cst (size_type_node);
    1989              :                 }
    1990              :             return true;
    1991              :           }
    1992              :         return false;
    1993              : 
    1994              :       case GIMPLE_PHI:
    1995              :         /* Unless RKIND == SRK_LENRANGE, all arguments of the PHI node
    1996              :            must have a constant length.  */
    1997        82620 :         for (unsigned i = 0; i < gimple_phi_num_args (def_stmt); i++)
    1998              :           {
    1999        60054 :             tree arg = gimple_phi_arg (def_stmt, i)->def;
    2000              : 
    2001              :             /* If this PHI has itself as an argument, we cannot
    2002              :                determine the string length of this argument.  However,
    2003              :                if we can find a constant string length for the other
    2004              :                PHI args then we can still be sure that this is a
    2005              :                constant string length.  So be optimistic and just
    2006              :                continue with the next argument.  */
    2007        60054 :             if (arg == gimple_phi_result (def_stmt))
    2008            0 :               continue;
    2009              : 
    2010        60054 :             if (!get_range_strlen (arg, visited, rkind, pdata, eltsize))
    2011              :               {
    2012        28016 :                 if (rkind != SRK_LENRANGE)
    2013              :                   return false;
    2014              :                 /* Set the upper bound to the maximum to prevent
    2015              :                    it from being adjusted in the next iteration but
    2016              :                    leave MINLEN and the more conservative MAXBOUND
    2017              :                    determined so far alone (or leave them null if
    2018              :                    they haven't been set yet).  That the MINLEN is
    2019              :                    in fact zero can be determined from MAXLEN being
    2020              :                    unbounded but the discovered minimum is used for
    2021              :                    diagnostics.  */
    2022        26245 :                 pdata->maxlen = build_all_ones_cst (size_type_node);
    2023              :               }
    2024              :           }
    2025              :         return true;
    2026              : 
    2027              :       default:
    2028              :         return false;
    2029              :     }
    2030              : }
    2031              : 
    2032              : /* Try to obtain the range of the lengths of the string(s) referenced
    2033              :    by ARG, or the size of the largest array ARG refers to if the range
    2034              :    of lengths cannot be determined, and store all in *PDATA which must
    2035              :    be zero-initialized on input except PDATA->MAXBOUND may be set to
    2036              :    a non-null tree node other than INTEGER_CST to request to have it
    2037              :    set to the length of the longest string in a PHI.  ELTSIZE is
    2038              :    the expected size of the string element in bytes: 1 for char and
    2039              :    some power of 2 for wide characters.
    2040              :    Return true if the range [PDATA->MINLEN, PDATA->MAXLEN] is suitable
    2041              :    for optimization.  Returning false means that a nonzero PDATA->MINLEN
    2042              :    doesn't reflect the true lower bound of the range when PDATA->MAXLEN
    2043              :    is -1 (in that case, the actual range is indeterminate, i.e.,
    2044              :    [0, PTRDIFF_MAX - 2].  */
    2045              : 
    2046              : bool
    2047      1136714 : get_range_strlen (tree arg, c_strlen_data *pdata, unsigned eltsize)
    2048              : {
    2049      1136714 :   auto_bitmap visited;
    2050      1136714 :   tree maxbound = pdata->maxbound;
    2051              : 
    2052      1136714 :   if (!get_range_strlen (arg, visited, SRK_LENRANGE, pdata, eltsize))
    2053              :     {
    2054              :       /* On failure extend the length range to an impossible maximum
    2055              :          (a valid MAXLEN must be less than PTRDIFF_MAX - 1).  Other
    2056              :          members can stay unchanged regardless.  */
    2057       912945 :       pdata->minlen = ssize_int (0);
    2058       912945 :       pdata->maxlen = build_all_ones_cst (size_type_node);
    2059              :     }
    2060       223769 :   else if (!pdata->minlen)
    2061         8386 :     pdata->minlen = ssize_int (0);
    2062              : 
    2063              :   /* If it's unchanged from it initial non-null value, set the conservative
    2064              :      MAXBOUND to SIZE_MAX.  Otherwise leave it null (if it is null).  */
    2065      1136714 :   if (maxbound && pdata->maxbound == maxbound)
    2066       650892 :     pdata->maxbound = build_all_ones_cst (size_type_node);
    2067              : 
    2068      1136714 :   return !integer_all_onesp (pdata->maxlen);
    2069      1136714 : }
    2070              : 
    2071              : /* Return the maximum value for ARG given RKIND (see strlen_range_kind).
    2072              :    For ARG of pointer types, NONSTR indicates if the caller is prepared
    2073              :    to handle unterminated strings.   For integer ARG and when RKIND ==
    2074              :    SRK_INT_VALUE, NONSTR must be null.
    2075              : 
    2076              :    If an unterminated array is discovered and our caller handles
    2077              :    unterminated arrays, then bubble up the offending DECL and
    2078              :    return the maximum size.  Otherwise return NULL.  */
    2079              : 
    2080              : static tree
    2081        96286 : get_maxval_strlen (tree arg, strlen_range_kind rkind, tree *nonstr = NULL)
    2082              : {
    2083              :   /* A non-null NONSTR is meaningless when determining the maximum
    2084              :      value of an integer ARG.  */
    2085        96286 :   gcc_assert (rkind != SRK_INT_VALUE || nonstr == NULL);
    2086              : 
    2087              :   // If arg is already a constant, simply return it.
    2088        96286 :   if (TREE_CODE (arg) == INTEGER_CST && rkind == SRK_INT_VALUE)
    2089              :     return arg;
    2090              : 
    2091              :   /* ARG must have an integral type when RKIND says so.  */
    2092        73359 :   gcc_assert (rkind != SRK_INT_VALUE || INTEGRAL_TYPE_P (TREE_TYPE (arg)));
    2093              : 
    2094        73458 :   auto_bitmap visited;
    2095              : 
    2096              :   /* Reset DATA.MAXLEN if the call fails or when DATA.MAXLEN
    2097              :      is unbounded.  */
    2098        73458 :   c_strlen_data lendata = { };
    2099        73458 :   if (!get_range_strlen (arg, visited, rkind, &lendata, /* eltsize = */1))
    2100        50153 :     lendata.maxlen = NULL_TREE;
    2101        23305 :   else if (lendata.maxlen && integer_all_onesp (lendata.maxlen))
    2102            0 :     lendata.maxlen = NULL_TREE;
    2103              : 
    2104        73458 :   if (nonstr)
    2105              :     {
    2106              :       /* For callers prepared to handle unterminated arrays set
    2107              :          *NONSTR to point to the declaration of the array and return
    2108              :          the maximum length/size. */
    2109        24201 :       *nonstr = lendata.decl;
    2110        24201 :       return lendata.maxlen;
    2111              :     }
    2112              : 
    2113              :   /* Fail if the constant array isn't nul-terminated.  */
    2114        49257 :   return lendata.decl ? NULL_TREE : lendata.maxlen;
    2115        73458 : }
    2116              : 
    2117              : /* Return true if LEN is known to be less than or equal to (or if STRICT is
    2118              :    true, strictly less than) the lower bound of SIZE at compile time and false
    2119              :    otherwise.  */
    2120              : 
    2121              : static bool
    2122        63260 : known_lower (gimple *stmt, tree len, tree size, bool strict = false)
    2123              : {
    2124        63260 :   if (len == NULL_TREE)
    2125              :     return false;
    2126              : 
    2127       236290 :   wide_int size_range[2];
    2128       236290 :   wide_int len_range[2];
    2129        47258 :   if (get_range (len, stmt, len_range) && get_range (size, stmt, size_range))
    2130              :     {
    2131        16555 :       if (strict)
    2132         1867 :         return wi::ltu_p (len_range[1], size_range[0]);
    2133              :       else
    2134        14688 :        return wi::leu_p (len_range[1], size_range[0]);
    2135              :     }
    2136              : 
    2137              :   return false;
    2138       283548 : }
    2139              : 
    2140              : /* Fold function call to builtin strcpy with arguments DEST and SRC.
    2141              :    If LEN is not NULL, it represents the length of the string to be
    2142              :    copied.  Return NULL_TREE if no simplification can be made.  */
    2143              : 
    2144              : static bool
    2145        26263 : gimple_fold_builtin_strcpy (gimple_stmt_iterator *gsi,
    2146              :                             tree dest, tree src)
    2147              : {
    2148        26263 :   gimple *stmt = gsi_stmt (*gsi);
    2149        26263 :   location_t loc = gimple_location (stmt);
    2150        26263 :   tree fn;
    2151              : 
    2152              :   /* If SRC and DEST are the same (and not volatile), return DEST.  */
    2153        26263 :   if (operand_equal_p (src, dest, 0))
    2154              :     {
    2155              :       /* Issue -Wrestrict unless the pointers are null (those do
    2156              :          not point to objects and so do not indicate an overlap;
    2157              :          such calls could be the result of sanitization and jump
    2158              :          threading).  */
    2159           86 :       if (!integer_zerop (dest) && !warning_suppressed_p (stmt, OPT_Wrestrict))
    2160              :         {
    2161           51 :           tree func = gimple_call_fndecl (stmt);
    2162              : 
    2163           51 :           warning_at (loc, OPT_Wrestrict,
    2164              :                       "%qD source argument is the same as destination",
    2165              :                       func);
    2166              :         }
    2167              : 
    2168           86 :       replace_call_with_value (gsi, dest);
    2169           86 :       return true;
    2170              :     }
    2171              : 
    2172        26177 :   if (optimize_function_for_size_p (cfun))
    2173              :     return false;
    2174              : 
    2175        24201 :   fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
    2176        24201 :   if (!fn)
    2177              :     return false;
    2178              : 
    2179              :   /* Set to non-null if ARG refers to an unterminated array.  */
    2180        24201 :   tree nonstr = NULL;
    2181        24201 :   tree len = get_maxval_strlen (src, SRK_STRLEN, &nonstr);
    2182              : 
    2183        24201 :   if (nonstr)
    2184              :     {
    2185              :       /* Avoid folding calls with unterminated arrays.  */
    2186          531 :       if (!warning_suppressed_p (stmt, OPT_Wstringop_overread))
    2187           69 :         warn_string_no_nul (loc, stmt, "strcpy", src, nonstr);
    2188          531 :       suppress_warning (stmt, OPT_Wstringop_overread);
    2189          531 :       return false;
    2190              :     }
    2191              : 
    2192        26569 :   if (!len || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
    2193              :     return false;
    2194              : 
    2195         2899 :   len = fold_convert_loc (loc, size_type_node, len);
    2196         2899 :   len = size_binop_loc (loc, PLUS_EXPR, len, build_int_cst (size_type_node, 1));
    2197         2899 :   len = force_gimple_operand_gsi (gsi, len, true,
    2198              :                                   NULL_TREE, true, GSI_SAME_STMT);
    2199         2899 :   gimple *repl = gimple_build_call (fn, 3, dest, src, len);
    2200         2899 :   replace_call_with_call_and_fold (gsi, repl);
    2201         2899 :   return true;
    2202              : }
    2203              : 
    2204              : /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
    2205              :    If SLEN is not NULL, it represents the length of the source string.
    2206              :    Return NULL_TREE if no simplification can be made.  */
    2207              : 
    2208              : static bool
    2209        17276 : gimple_fold_builtin_strncpy (gimple_stmt_iterator *gsi,
    2210              :                              tree dest, tree src, tree len)
    2211              : {
    2212        17276 :   gimple *stmt = gsi_stmt (*gsi);
    2213        17276 :   location_t loc = gimple_location (stmt);
    2214        17276 :   bool nonstring = get_attr_nonstring_decl (dest) != NULL_TREE;
    2215              : 
    2216              :   /* If the LEN parameter is zero, return DEST.  */
    2217        17276 :   if (integer_zerop (len))
    2218              :     {
    2219              :       /* Avoid warning if the destination refers to an array/pointer
    2220              :          decorate with attribute nonstring.  */
    2221          167 :       if (!nonstring)
    2222              :         {
    2223          155 :           tree fndecl = gimple_call_fndecl (stmt);
    2224              : 
    2225              :           /* Warn about the lack of nul termination: the result is not
    2226              :              a (nul-terminated) string.  */
    2227          155 :           tree slen = get_maxval_strlen (src, SRK_STRLEN);
    2228          155 :           if (slen && !integer_zerop (slen))
    2229           24 :             warning_at (loc, OPT_Wstringop_truncation,
    2230              :                         "%qD destination unchanged after copying no bytes "
    2231              :                         "from a string of length %E",
    2232              :                         fndecl, slen);
    2233              :           else
    2234          131 :             warning_at (loc, OPT_Wstringop_truncation,
    2235              :                         "%qD destination unchanged after copying no bytes",
    2236              :                         fndecl);
    2237              :         }
    2238              : 
    2239          167 :       replace_call_with_value (gsi, dest);
    2240          167 :       return true;
    2241              :     }
    2242              : 
    2243              :   /* We can't compare slen with len as constants below if len is not a
    2244              :      constant.  */
    2245        17109 :   if (TREE_CODE (len) != INTEGER_CST)
    2246              :     return false;
    2247              : 
    2248              :   /* Now, we must be passed a constant src ptr parameter.  */
    2249        10750 :   tree slen = get_maxval_strlen (src, SRK_STRLEN);
    2250        10750 :   if (!slen || TREE_CODE (slen) != INTEGER_CST)
    2251              :     return false;
    2252              : 
    2253              :   /* The size of the source string including the terminating nul.  */
    2254         1780 :   tree ssize = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
    2255              : 
    2256              :   /* We do not support simplification of this case, though we do
    2257              :      support it when expanding trees into RTL.  */
    2258              :   /* FIXME: generate a call to __builtin_memset.  */
    2259         1780 :   if (tree_int_cst_lt (ssize, len))
    2260              :     return false;
    2261              : 
    2262              :   /* Diagnose truncation that leaves the copy unterminated.  */
    2263          695 :   maybe_diag_stxncpy_trunc (*gsi, src, len);
    2264              : 
    2265              :   /* OK transform into builtin memcpy.  */
    2266          695 :   tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
    2267         1390 :   if (!fn || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
    2268              :     return false;
    2269              : 
    2270          695 :   len = fold_convert_loc (loc, size_type_node, len);
    2271          695 :   len = force_gimple_operand_gsi (gsi, len, true,
    2272              :                                   NULL_TREE, true, GSI_SAME_STMT);
    2273          695 :   gimple *repl = gimple_build_call (fn, 3, dest, src, len);
    2274          695 :   replace_call_with_call_and_fold (gsi, repl);
    2275              : 
    2276          695 :   return true;
    2277              : }
    2278              : 
    2279              : /* Fold function call to builtin strchr or strrchr.
    2280              :    If both arguments are constant, evaluate and fold the result,
    2281              :    otherwise simplify str(r)chr (str, 0) into str + strlen (str).
    2282              :    In general strlen is significantly faster than strchr
    2283              :    due to being a simpler operation.  */
    2284              : static bool
    2285         5476 : gimple_fold_builtin_strchr (gimple_stmt_iterator *gsi, bool is_strrchr)
    2286              : {
    2287         5476 :   gimple *stmt = gsi_stmt (*gsi);
    2288         5476 :   tree str = gimple_call_arg (stmt, 0);
    2289         5476 :   tree c = gimple_call_arg (stmt, 1);
    2290         5476 :   location_t loc = gimple_location (stmt);
    2291         5476 :   const char *p;
    2292         5476 :   char ch;
    2293              : 
    2294         5476 :   if (!gimple_call_lhs (stmt))
    2295              :     return false;
    2296              : 
    2297              :   /* Avoid folding if the first argument is not a nul-terminated array.
    2298              :      Defer warning until later.  */
    2299         5466 :   if (!check_nul_terminated_array (NULL_TREE, str))
    2300              :     return false;
    2301              : 
    2302         5382 :   if ((p = c_getstr (str)) && target_char_cst_p (c, &ch))
    2303              :     {
    2304           41 :       const char *p1 = is_strrchr ? strrchr (p, ch) : strchr (p, ch);
    2305              : 
    2306           41 :       if (p1 == NULL)
    2307              :         {
    2308            1 :           replace_call_with_value (gsi, integer_zero_node);
    2309            1 :           return true;
    2310              :         }
    2311              : 
    2312           40 :       tree len = build_int_cst (size_type_node, p1 - p);
    2313           40 :       gimple_seq stmts = NULL;
    2314           40 :       gimple *new_stmt = gimple_build_assign (gimple_call_lhs (stmt),
    2315              :                                               POINTER_PLUS_EXPR, str, len);
    2316           40 :       gimple_seq_add_stmt_without_update (&stmts, new_stmt);
    2317           40 :       gsi_replace_with_seq_vops (gsi, stmts);
    2318           40 :       return true;
    2319              :     }
    2320              : 
    2321         5423 :   if (!integer_zerop (c) || (!gimple_vuse (stmt) && gimple_in_ssa_p (cfun)))
    2322              :     return false;
    2323              : 
    2324              :   /* Transform strrchr (s, 0) to strchr (s, 0) when optimizing for size.  */
    2325           82 :   if (is_strrchr && optimize_function_for_size_p (cfun))
    2326              :     {
    2327            3 :       tree strchr_fn = builtin_decl_implicit (BUILT_IN_STRCHR);
    2328              : 
    2329            3 :       if (strchr_fn)
    2330              :         {
    2331            3 :           gimple *repl = gimple_build_call (strchr_fn, 2, str, c);
    2332            3 :           replace_call_with_call_and_fold (gsi, repl);
    2333            3 :           return true;
    2334              :         }
    2335              : 
    2336              :       return false;
    2337              :     }
    2338              : 
    2339           79 :   tree len;
    2340           79 :   tree strlen_fn = builtin_decl_implicit (BUILT_IN_STRLEN);
    2341              : 
    2342           79 :   if (!strlen_fn)
    2343              :     return false;
    2344              : 
    2345              :   /* Create newstr = strlen (str).  */
    2346           79 :   gimple_seq stmts = NULL;
    2347           79 :   gimple *new_stmt = gimple_build_call (strlen_fn, 1, str);
    2348           79 :   gimple_set_location (new_stmt, loc);
    2349           79 :   len = make_ssa_name (size_type_node);
    2350           79 :   gimple_call_set_lhs (new_stmt, len);
    2351           79 :   gimple_seq_add_stmt_without_update (&stmts, new_stmt);
    2352              : 
    2353              :   /* Create (str p+ strlen (str)).  */
    2354           79 :   new_stmt = gimple_build_assign (gimple_call_lhs (stmt),
    2355              :                                   POINTER_PLUS_EXPR, str, len);
    2356           79 :   gimple_seq_add_stmt_without_update (&stmts, new_stmt);
    2357           79 :   gsi_replace_with_seq_vops (gsi, stmts);
    2358              :   /* gsi now points at the assignment to the lhs, get a
    2359              :      stmt iterator to the strlen.
    2360              :      ???  We can't use gsi_for_stmt as that doesn't work when the
    2361              :      CFG isn't built yet.  */
    2362           79 :   gimple_stmt_iterator gsi2 = *gsi;
    2363           79 :   gsi_prev (&gsi2);
    2364           79 :   fold_stmt (&gsi2);
    2365           79 :   return true;
    2366              : }
    2367              : 
    2368              : /* Fold function call to builtin strstr.
    2369              :    If both arguments are constant, evaluate and fold the result,
    2370              :    additionally fold strstr (x, "") into x and strstr (x, "c")
    2371              :    into strchr (x, 'c').  */
    2372              : static bool
    2373         4347 : gimple_fold_builtin_strstr (gimple_stmt_iterator *gsi)
    2374              : {
    2375         4347 :   gimple *stmt = gsi_stmt (*gsi);
    2376         4347 :   if (!gimple_call_lhs (stmt))
    2377              :     return false;
    2378              : 
    2379         4344 :   tree haystack = gimple_call_arg (stmt, 0);
    2380         4344 :   tree needle = gimple_call_arg (stmt, 1);
    2381              : 
    2382              :   /* Avoid folding if either argument is not a nul-terminated array.
    2383              :      Defer warning until later.  */
    2384         4344 :   if (!check_nul_terminated_array (NULL_TREE, haystack)
    2385         4344 :       || !check_nul_terminated_array (NULL_TREE, needle))
    2386              :     return false;
    2387              : 
    2388         4325 :   const char *q = c_getstr (needle);
    2389         4325 :   if (q == NULL)
    2390              :     return false;
    2391              : 
    2392         3151 :   if (const char *p = c_getstr (haystack))
    2393              :     {
    2394           14 :       const char *r = strstr (p, q);
    2395              : 
    2396           14 :       if (r == NULL)
    2397              :         {
    2398            1 :           replace_call_with_value (gsi, integer_zero_node);
    2399            1 :           return true;
    2400              :         }
    2401              : 
    2402           13 :       tree len = build_int_cst (size_type_node, r - p);
    2403           13 :       gimple_seq stmts = NULL;
    2404           13 :       gimple *new_stmt
    2405           13 :         = gimple_build_assign (gimple_call_lhs (stmt), POINTER_PLUS_EXPR,
    2406              :                                haystack, len);
    2407           13 :       gimple_seq_add_stmt_without_update (&stmts, new_stmt);
    2408           13 :       gsi_replace_with_seq_vops (gsi, stmts);
    2409           13 :       return true;
    2410              :     }
    2411              : 
    2412              :   /* For strstr (x, "") return x.  */
    2413         3137 :   if (q[0] == '\0')
    2414              :     {
    2415            6 :       replace_call_with_value (gsi, haystack);
    2416            6 :       return true;
    2417              :     }
    2418              : 
    2419         6262 :   if (!gimple_vuse (stmt) && gimple_in_ssa_p (cfun))
    2420              :     return false;
    2421              : 
    2422              :   /* Transform strstr (x, "c") into strchr (x, 'c').  */
    2423         3131 :   if (q[1] == '\0')
    2424              :     {
    2425           22 :       tree strchr_fn = builtin_decl_implicit (BUILT_IN_STRCHR);
    2426           22 :       if (strchr_fn)
    2427              :         {
    2428           22 :           tree c = build_int_cst (integer_type_node, q[0]);
    2429           22 :           gimple *repl = gimple_build_call (strchr_fn, 2, haystack, c);
    2430           22 :           replace_call_with_call_and_fold (gsi, repl);
    2431           22 :           return true;
    2432              :         }
    2433              :     }
    2434              : 
    2435              :   return false;
    2436              : }
    2437              : 
    2438              : /* Simplify a call to the strcat builtin.  DST and SRC are the arguments
    2439              :    to the call.
    2440              : 
    2441              :    Return NULL_TREE if no simplification was possible, otherwise return the
    2442              :    simplified form of the call as a tree.
    2443              : 
    2444              :    The simplified form may be a constant or other expression which
    2445              :    computes the same value, but in a more efficient manner (including
    2446              :    calls to other builtin functions).
    2447              : 
    2448              :    The call may contain arguments which need to be evaluated, but
    2449              :    which are not useful to determine the result of the call.  In
    2450              :    this case we return a chain of COMPOUND_EXPRs.  The LHS of each
    2451              :    COMPOUND_EXPR will be an argument which must be evaluated.
    2452              :    COMPOUND_EXPRs are chained through their RHS.  The RHS of the last
    2453              :    COMPOUND_EXPR in the chain will contain the tree for the simplified
    2454              :    form of the builtin function call.  */
    2455              : 
    2456              : static bool
    2457         7464 : gimple_fold_builtin_strcat (gimple_stmt_iterator *gsi, tree dst, tree src)
    2458              : {
    2459         7464 :   gimple *stmt = gsi_stmt (*gsi);
    2460         7464 :   location_t loc = gimple_location (stmt);
    2461              : 
    2462         7464 :   const char *p = c_getstr (src);
    2463              : 
    2464              :   /* If the string length is zero, return the dst parameter.  */
    2465         7464 :   if (p && *p == '\0')
    2466              :     {
    2467           72 :       replace_call_with_value (gsi, dst);
    2468           72 :       return true;
    2469              :     }
    2470              : 
    2471         7392 :   if (!optimize_bb_for_speed_p (gimple_bb (stmt)))
    2472              :     return false;
    2473              : 
    2474        13610 :   if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
    2475              :     return false;
    2476              : 
    2477              :   /* See if we can store by pieces into (dst + strlen(dst)).  */
    2478         6805 :   tree newdst;
    2479         6805 :   tree strlen_fn = builtin_decl_implicit (BUILT_IN_STRLEN);
    2480         6805 :   tree memcpy_fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
    2481              : 
    2482         6805 :   if (!strlen_fn || !memcpy_fn)
    2483              :     return false;
    2484              : 
    2485              :   /* If the length of the source string isn't computable don't
    2486              :      split strcat into strlen and memcpy.  */
    2487         6805 :   tree len = get_maxval_strlen (src, SRK_STRLEN);
    2488         6805 :   if (! len)
    2489              :     return false;
    2490              : 
    2491              :   /* Create strlen (dst).  */
    2492          828 :   gimple_seq stmts = NULL, stmts2;
    2493          828 :   gimple *repl = gimple_build_call (strlen_fn, 1, dst);
    2494          828 :   gimple_set_location (repl, loc);
    2495          828 :   newdst = make_ssa_name (size_type_node);
    2496          828 :   gimple_call_set_lhs (repl, newdst);
    2497          828 :   gimple_seq_add_stmt_without_update (&stmts, repl);
    2498              : 
    2499              :   /* Create (dst p+ strlen (dst)).  */
    2500          828 :   newdst = fold_build_pointer_plus_loc (loc, dst, newdst);
    2501          828 :   newdst = force_gimple_operand (newdst, &stmts2, true, NULL_TREE);
    2502          828 :   gimple_seq_add_seq_without_update (&stmts, stmts2);
    2503              : 
    2504          828 :   len = fold_convert_loc (loc, size_type_node, len);
    2505          828 :   len = size_binop_loc (loc, PLUS_EXPR, len,
    2506              :                         build_int_cst (size_type_node, 1));
    2507          828 :   len = force_gimple_operand (len, &stmts2, true, NULL_TREE);
    2508          828 :   gimple_seq_add_seq_without_update (&stmts, stmts2);
    2509              : 
    2510          828 :   repl = gimple_build_call (memcpy_fn, 3, newdst, src, len);
    2511          828 :   gimple_seq_add_stmt_without_update (&stmts, repl);
    2512          828 :   if (gimple_call_lhs (stmt))
    2513              :     {
    2514          165 :       repl = gimple_build_assign (gimple_call_lhs (stmt), dst);
    2515          165 :       gimple_seq_add_stmt_without_update (&stmts, repl);
    2516          165 :       gsi_replace_with_seq_vops (gsi, stmts);
    2517              :       /* gsi now points at the assignment to the lhs, get a
    2518              :          stmt iterator to the memcpy call.
    2519              :          ???  We can't use gsi_for_stmt as that doesn't work when the
    2520              :          CFG isn't built yet.  */
    2521          165 :       gimple_stmt_iterator gsi2 = *gsi;
    2522          165 :       gsi_prev (&gsi2);
    2523          165 :       fold_stmt (&gsi2);
    2524              :     }
    2525              :   else
    2526              :     {
    2527          663 :       gsi_replace_with_seq_vops (gsi, stmts);
    2528          663 :       fold_stmt (gsi);
    2529              :     }
    2530              :   return true;
    2531              : }
    2532              : 
    2533              : /* Fold a call to the __strcat_chk builtin FNDECL.  DEST, SRC, and SIZE
    2534              :    are the arguments to the call.  */
    2535              : 
    2536              : static bool
    2537         1493 : gimple_fold_builtin_strcat_chk (gimple_stmt_iterator *gsi)
    2538              : {
    2539         1493 :   gimple *stmt = gsi_stmt (*gsi);
    2540         1493 :   tree dest = gimple_call_arg (stmt, 0);
    2541         1493 :   tree src = gimple_call_arg (stmt, 1);
    2542         1493 :   tree size = gimple_call_arg (stmt, 2);
    2543         1493 :   tree fn;
    2544         1493 :   const char *p;
    2545              : 
    2546         1493 :   p = c_getstr (src);
    2547              :   /* If the SRC parameter is "", return DEST.  */
    2548         1493 :   if (p && *p == '\0')
    2549              :     {
    2550           60 :       replace_call_with_value (gsi, dest);
    2551           60 :       return true;
    2552              :     }
    2553              : 
    2554         1433 :   if (! tree_fits_uhwi_p (size) || ! integer_all_onesp (size))
    2555              :     return false;
    2556              : 
    2557          164 :   if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
    2558              :     return false;
    2559              : 
    2560              :   /* If __builtin_strcat_chk is used, assume strcat is available.  */
    2561           82 :   fn = builtin_decl_explicit (BUILT_IN_STRCAT);
    2562           82 :   if (!fn)
    2563              :     return false;
    2564              : 
    2565           82 :   gimple *repl = gimple_build_call (fn, 2, dest, src);
    2566           82 :   replace_call_with_call_and_fold (gsi, repl);
    2567           82 :   return true;
    2568              : }
    2569              : 
    2570              : /* Simplify a call to the strncat builtin.  */
    2571              : 
    2572              : static bool
    2573         6786 : gimple_fold_builtin_strncat (gimple_stmt_iterator *gsi)
    2574              : {
    2575         6786 :   gimple *stmt = gsi_stmt (*gsi);
    2576         6786 :   tree dst = gimple_call_arg (stmt, 0);
    2577         6786 :   tree src = gimple_call_arg (stmt, 1);
    2578         6786 :   tree len = gimple_call_arg (stmt, 2);
    2579         6786 :   tree src_len = c_strlen (src, 1);
    2580              : 
    2581              :   /* If the requested length is zero, or the src parameter string
    2582              :      length is zero, return the dst parameter.  */
    2583         6786 :   if (integer_zerop (len) || (src_len && integer_zerop (src_len)))
    2584              :     {
    2585          119 :       replace_call_with_value (gsi, dst);
    2586          119 :       return true;
    2587              :     }
    2588              : 
    2589              :   /* Return early if the requested len is less than the string length.
    2590              :      Warnings will be issued elsewhere later.  */
    2591         6667 :   if (!src_len || known_lower (stmt, len, src_len, true))
    2592              :     return false;
    2593              : 
    2594              :   /* Warn on constant LEN.  */
    2595          568 :   if (TREE_CODE (len) == INTEGER_CST)
    2596              :     {
    2597          131 :       bool nowarn = warning_suppressed_p (stmt, OPT_Wstringop_overflow_);
    2598          131 :       tree dstsize;
    2599              : 
    2600          131 :       if (!nowarn && compute_builtin_object_size (dst, 1, &dstsize)
    2601          175 :           && TREE_CODE (dstsize) == INTEGER_CST)
    2602              :         {
    2603           44 :           int cmpdst = tree_int_cst_compare (len, dstsize);
    2604              : 
    2605           44 :           if (cmpdst >= 0)
    2606              :             {
    2607           19 :               tree fndecl = gimple_call_fndecl (stmt);
    2608              : 
    2609              :               /* Strncat copies (at most) LEN bytes and always appends
    2610              :                  the terminating NUL so the specified bound should never
    2611              :                  be equal to (or greater than) the size of the destination.
    2612              :                  If it is, the copy could overflow.  */
    2613           19 :               location_t loc = gimple_location (stmt);
    2614           37 :               nowarn = warning_at (loc, OPT_Wstringop_overflow_,
    2615              :                                    cmpdst == 0
    2616              :                                    ? G_("%qD specified bound %E equals "
    2617              :                                         "destination size")
    2618              :                                    : G_("%qD specified bound %E exceeds "
    2619              :                                         "destination size %E"),
    2620              :                                    fndecl, len, dstsize);
    2621           19 :               if (nowarn)
    2622            0 :                 suppress_warning (stmt, OPT_Wstringop_overflow_);
    2623              :             }
    2624              :         }
    2625              : 
    2626          131 :       if (!nowarn && TREE_CODE (src_len) == INTEGER_CST
    2627          243 :           && tree_int_cst_compare (src_len, len) == 0)
    2628              :         {
    2629           20 :           tree fndecl = gimple_call_fndecl (stmt);
    2630           20 :           location_t loc = gimple_location (stmt);
    2631              : 
    2632              :           /* To avoid possible overflow the specified bound should also
    2633              :              not be equal to the length of the source, even when the size
    2634              :              of the destination is unknown (it's not an uncommon mistake
    2635              :              to specify as the bound to strncpy the length of the source).  */
    2636           20 :           if (warning_at (loc, OPT_Wstringop_overflow_,
    2637              :                           "%qD specified bound %E equals source length",
    2638              :                           fndecl, len))
    2639            6 :             suppress_warning (stmt, OPT_Wstringop_overflow_);
    2640              :         }
    2641              :     }
    2642              : 
    2643          568 :   if (!known_lower (stmt, src_len, len))
    2644              :     return false;
    2645              : 
    2646          136 :   tree fn = builtin_decl_implicit (BUILT_IN_STRCAT);
    2647              : 
    2648              :   /* If the replacement _DECL isn't initialized, don't do the
    2649              :      transformation.  */
    2650          272 :   if (!fn || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
    2651              :     return false;
    2652              : 
    2653              :   /* Otherwise, emit a call to strcat.  */
    2654          136 :   gcall *repl = gimple_build_call (fn, 2, dst, src);
    2655          136 :   replace_call_with_call_and_fold (gsi, repl);
    2656          136 :   return true;
    2657              : }
    2658              : 
    2659              : /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
    2660              :    LEN, and SIZE.  */
    2661              : 
    2662              : static bool
    2663         1116 : gimple_fold_builtin_strncat_chk (gimple_stmt_iterator *gsi)
    2664              : {
    2665         1116 :   gimple *stmt = gsi_stmt (*gsi);
    2666         1116 :   tree dest = gimple_call_arg (stmt, 0);
    2667         1116 :   tree src = gimple_call_arg (stmt, 1);
    2668         1116 :   tree len = gimple_call_arg (stmt, 2);
    2669         1116 :   tree size = gimple_call_arg (stmt, 3);
    2670         1116 :   tree fn;
    2671         1116 :   const char *p;
    2672              : 
    2673         1116 :   p = c_getstr (src);
    2674              :   /* If the SRC parameter is "" or if LEN is 0, return DEST.  */
    2675          276 :   if ((p && *p == '\0')
    2676         1341 :       || integer_zerop (len))
    2677              :     {
    2678           78 :       replace_call_with_value (gsi, dest);
    2679           78 :       return true;
    2680              :     }
    2681              : 
    2682         2076 :   if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
    2683              :     return false;
    2684              : 
    2685         1038 :   if (! integer_all_onesp (size))
    2686              :     {
    2687          951 :       tree src_len = c_strlen (src, 1);
    2688          951 :       if (known_lower (stmt, src_len, len))
    2689              :         {
    2690              :           /* If LEN >= strlen (SRC), optimize into __strcat_chk.  */
    2691           39 :           fn = builtin_decl_explicit (BUILT_IN_STRCAT_CHK);
    2692           39 :           if (!fn)
    2693              :             return false;
    2694              : 
    2695           39 :           gimple *repl = gimple_build_call (fn, 3, dest, src, size);
    2696           39 :           replace_call_with_call_and_fold (gsi, repl);
    2697           39 :           return true;
    2698              :         }
    2699              :       return false;
    2700              :     }
    2701              : 
    2702              :   /* If __builtin_strncat_chk is used, assume strncat is available.  */
    2703           87 :   fn = builtin_decl_explicit (BUILT_IN_STRNCAT);
    2704           87 :   if (!fn)
    2705              :     return false;
    2706              : 
    2707           87 :   gimple *repl = gimple_build_call (fn, 3, dest, src, len);
    2708           87 :   replace_call_with_call_and_fold (gsi, repl);
    2709           87 :   return true;
    2710              : }
    2711              : 
    2712              : /* Build and append gimple statements to STMTS that would load a first
    2713              :    character of a memory location identified by STR.  LOC is location
    2714              :    of the statement.  */
    2715              : 
    2716              : static tree
    2717          482 : gimple_load_first_char (location_t loc, tree str, gimple_seq *stmts)
    2718              : {
    2719          482 :   tree var;
    2720              : 
    2721          482 :   tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
    2722          482 :   tree cst_uchar_ptr_node
    2723          482 :     = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
    2724          482 :   tree off0 = build_int_cst (cst_uchar_ptr_node, 0);
    2725              : 
    2726          482 :   tree temp = fold_build2_loc (loc, MEM_REF, cst_uchar_node, str, off0);
    2727          482 :   gassign *stmt = gimple_build_assign (NULL_TREE, temp);
    2728          482 :   var = make_ssa_name (cst_uchar_node, stmt);
    2729              : 
    2730          482 :   gimple_assign_set_lhs (stmt, var);
    2731          482 :   gimple_seq_add_stmt_without_update (stmts, stmt);
    2732              : 
    2733          482 :   return var;
    2734              : }
    2735              : 
    2736              : /* Fold a call to the str{n}{case}cmp builtin pointed by GSI iterator.  */
    2737              : 
    2738              : static bool
    2739      1251175 : gimple_fold_builtin_string_compare (gimple_stmt_iterator *gsi)
    2740              : {
    2741      1251175 :   gimple *stmt = gsi_stmt (*gsi);
    2742      1251175 :   tree callee = gimple_call_fndecl (stmt);
    2743      1251175 :   enum built_in_function fcode = DECL_FUNCTION_CODE (callee);
    2744              : 
    2745      1251175 :   tree type = integer_type_node;
    2746      1251175 :   tree str1 = gimple_call_arg (stmt, 0);
    2747      1251175 :   tree str2 = gimple_call_arg (stmt, 1);
    2748      1251175 :   tree lhs = gimple_call_lhs (stmt);
    2749              : 
    2750      1251175 :   tree bound_node = NULL_TREE;
    2751      1251175 :   unsigned HOST_WIDE_INT bound = HOST_WIDE_INT_M1U;
    2752              : 
    2753              :   /* Handle strncmp and strncasecmp functions.  */
    2754      1251175 :   if (gimple_call_num_args (stmt) == 3)
    2755              :     {
    2756        23375 :       bound_node = gimple_call_arg (stmt, 2);
    2757        23375 :       if (tree_fits_uhwi_p (bound_node))
    2758        17648 :         bound = tree_to_uhwi (bound_node);
    2759              :     }
    2760              : 
    2761              :   /* If the BOUND parameter is zero, return zero.  */
    2762        17648 :   if (bound == 0)
    2763              :     {
    2764            4 :       replace_call_with_value (gsi, integer_zero_node);
    2765            4 :       return true;
    2766              :     }
    2767              : 
    2768              :   /* If ARG1 and ARG2 are the same (and not volatile), return zero.  */
    2769      1251171 :   if (operand_equal_p (str1, str2, 0))
    2770              :     {
    2771           41 :       replace_call_with_value (gsi, integer_zero_node);
    2772           41 :       return true;
    2773              :     }
    2774              : 
    2775      2502260 :   if (!gimple_vuse (stmt) && gimple_in_ssa_p (cfun))
    2776              :     return false;
    2777              : 
    2778              :   /* Initially set to the number of characters, including the terminating
    2779              :      nul if each array has one.   LENx == strnlen (Sx, LENx) implies that
    2780              :      the array Sx is not terminated by a nul.
    2781              :      For nul-terminated strings then adjusted to their length so that
    2782              :      LENx == NULPOSx holds.  */
    2783      1251130 :   unsigned HOST_WIDE_INT len1 = HOST_WIDE_INT_MAX, len2 = len1;
    2784      1251130 :   const char *p1 = getbyterep (str1, &len1);
    2785      1251130 :   const char *p2 = getbyterep (str2, &len2);
    2786              : 
    2787              :   /* The position of the terminating nul character if one exists, otherwise
    2788              :      a value greater than LENx.  */
    2789      1251130 :   unsigned HOST_WIDE_INT nulpos1 = HOST_WIDE_INT_MAX, nulpos2 = nulpos1;
    2790              : 
    2791      1251130 :   if (p1)
    2792              :     {
    2793        42819 :       size_t n = strnlen (p1, len1);
    2794        42819 :       if (n < len1)
    2795        42712 :         len1 = nulpos1 = n;
    2796              :     }
    2797              : 
    2798      1251130 :   if (p2)
    2799              :     {
    2800      1220029 :       size_t n = strnlen (p2, len2);
    2801      1220029 :       if (n < len2)
    2802      1219970 :         len2 = nulpos2 = n;
    2803              :     }
    2804              : 
    2805              :   /* For known strings, return an immediate value.  */
    2806      1251130 :   if (p1 && p2)
    2807              :     {
    2808        39245 :       int r = 0;
    2809        39245 :       bool known_result = false;
    2810              : 
    2811        39245 :       switch (fcode)
    2812              :         {
    2813        38169 :         case BUILT_IN_STRCMP:
    2814        38169 :         case BUILT_IN_STRCMP_EQ:
    2815        38169 :           if (len1 != nulpos1 || len2 != nulpos2)
    2816              :             break;
    2817              : 
    2818        38144 :           r = strcmp (p1, p2);
    2819        38144 :           known_result = true;
    2820        38144 :           break;
    2821              : 
    2822         1002 :         case BUILT_IN_STRNCMP:
    2823         1002 :         case BUILT_IN_STRNCMP_EQ:
    2824         1002 :           {
    2825         1002 :             if (bound == HOST_WIDE_INT_M1U)
    2826              :               break;
    2827              : 
    2828              :             /* Reduce the bound to be no more than the length
    2829              :                of the shorter of the two strings, or the sizes
    2830              :                of the unterminated arrays.  */
    2831           38 :             unsigned HOST_WIDE_INT n = bound;
    2832              : 
    2833           38 :             if (len1 == nulpos1 && len1 < n)
    2834            4 :               n = len1 + 1;
    2835           38 :             if (len2 == nulpos2 && len2 < n)
    2836           11 :               n = len2 + 1;
    2837              : 
    2838           38 :             if (MIN (nulpos1, nulpos2) + 1 < n)
    2839              :               break;
    2840              : 
    2841           38 :             r = strncmp (p1, p2, n);
    2842           38 :             known_result = true;
    2843           38 :             break;
    2844              :           }
    2845              :         /* Only handleable situation is where the string are equal (result 0),
    2846              :            which is already handled by operand_equal_p case.  */
    2847              :         case BUILT_IN_STRCASECMP:
    2848              :           break;
    2849           37 :         case BUILT_IN_STRNCASECMP:
    2850           37 :           {
    2851           37 :             if (bound == HOST_WIDE_INT_M1U)
    2852              :               break;
    2853           37 :             r = strncmp (p1, p2, bound);
    2854           37 :             if (r == 0)
    2855              :               known_result = true;
    2856              :             break;
    2857              :           }
    2858            0 :         default:
    2859            0 :           gcc_unreachable ();
    2860              :         }
    2861              : 
    2862        38182 :       if (known_result)
    2863              :         {
    2864        38182 :           replace_call_with_value (gsi, build_cmp_result (type, r));
    2865        38182 :           return true;
    2866              :         }
    2867              :     }
    2868              : 
    2869      2425896 :   bool nonzero_bound = (bound >= 1 && bound < HOST_WIDE_INT_M1U)
    2870      1195444 :     || fcode == BUILT_IN_STRCMP
    2871      1195444 :     || fcode == BUILT_IN_STRCMP_EQ
    2872      1218865 :     || fcode == BUILT_IN_STRCASECMP;
    2873              : 
    2874      1212948 :   location_t loc = gimple_location (stmt);
    2875              : 
    2876              :   /* If the second arg is "", return *(const unsigned char*)arg1.  */
    2877      1212948 :   if (p2 && *p2 == '\0' && nonzero_bound)
    2878              :     {
    2879          150 :       gimple_seq stmts = NULL;
    2880          150 :       tree var = gimple_load_first_char (loc, str1, &stmts);
    2881          150 :       if (lhs)
    2882              :         {
    2883          150 :           stmt = gimple_build_assign (lhs, NOP_EXPR, var);
    2884          150 :           gimple_seq_add_stmt_without_update (&stmts, stmt);
    2885              :         }
    2886              : 
    2887          150 :       gsi_replace_with_seq_vops (gsi, stmts);
    2888          150 :       return true;
    2889              :     }
    2890              : 
    2891              :   /* If the first arg is "", return -*(const unsigned char*)arg2.  */
    2892      1212798 :   if (p1 && *p1 == '\0' && nonzero_bound)
    2893              :     {
    2894           99 :       gimple_seq stmts = NULL;
    2895           99 :       tree var = gimple_load_first_char (loc, str2, &stmts);
    2896              : 
    2897           99 :       if (lhs)
    2898              :         {
    2899           99 :           tree c = make_ssa_name (integer_type_node);
    2900           99 :           stmt = gimple_build_assign (c, NOP_EXPR, var);
    2901           99 :           gimple_seq_add_stmt_without_update (&stmts, stmt);
    2902              : 
    2903           99 :           stmt = gimple_build_assign (lhs, NEGATE_EXPR, c);
    2904           99 :           gimple_seq_add_stmt_without_update (&stmts, stmt);
    2905              :         }
    2906              : 
    2907           99 :       gsi_replace_with_seq_vops (gsi, stmts);
    2908           99 :       return true;
    2909              :     }
    2910              : 
    2911              :   /* If BOUND is one, return an expression corresponding to
    2912              :      (*(const unsigned char*)arg2 - *(const unsigned char*)arg1).  */
    2913      1212699 :   if (fcode == BUILT_IN_STRNCMP && bound == 1)
    2914              :     {
    2915          110 :       gimple_seq stmts = NULL;
    2916          110 :       tree temp1 = gimple_load_first_char (loc, str1, &stmts);
    2917          110 :       tree temp2 = gimple_load_first_char (loc, str2, &stmts);
    2918              : 
    2919          110 :       if (lhs)
    2920              :         {
    2921          107 :           tree c1 = make_ssa_name (integer_type_node);
    2922          107 :           gassign *convert1 = gimple_build_assign (c1, NOP_EXPR, temp1);
    2923          107 :           gimple_seq_add_stmt_without_update (&stmts, convert1);
    2924              : 
    2925          107 :           tree c2 = make_ssa_name (integer_type_node);
    2926          107 :           gassign *convert2 = gimple_build_assign (c2, NOP_EXPR, temp2);
    2927          107 :           gimple_seq_add_stmt_without_update (&stmts, convert2);
    2928              : 
    2929          107 :           stmt = gimple_build_assign (lhs, MINUS_EXPR, c1, c2);
    2930          107 :           gimple_seq_add_stmt_without_update (&stmts, stmt);
    2931              :         }
    2932              : 
    2933          110 :       gsi_replace_with_seq_vops (gsi, stmts);
    2934          110 :       return true;
    2935              :     }
    2936              : 
    2937              :   /* If BOUND is greater than the length of one constant string,
    2938              :      and the other argument is also a nul-terminated string, replace
    2939              :      strncmp with strcmp.  */
    2940      1212589 :   if (fcode == BUILT_IN_STRNCMP
    2941        17741 :       && bound > 0 && bound < HOST_WIDE_INT_M1U
    2942        12146 :       && ((p2 && len2 < bound && len2 == nulpos2)
    2943        11914 :           || (p1 && len1 < bound && len1 == nulpos1)))
    2944              :     {
    2945          310 :       tree fn = builtin_decl_implicit (BUILT_IN_STRCMP);
    2946          310 :       if (!fn)
    2947              :         return false;
    2948          310 :       gimple *repl = gimple_build_call (fn, 2, str1, str2);
    2949          310 :       replace_call_with_call_and_fold (gsi, repl);
    2950          310 :       return true;
    2951              :     }
    2952              : 
    2953              :   return false;
    2954              : }
    2955              : 
    2956              : /* Fold a call to the memchr pointed by GSI iterator.  */
    2957              : 
    2958              : static bool
    2959        29511 : gimple_fold_builtin_memchr (gimple_stmt_iterator *gsi)
    2960              : {
    2961        29511 :   gimple *stmt = gsi_stmt (*gsi);
    2962        29511 :   tree lhs = gimple_call_lhs (stmt);
    2963        29511 :   tree arg1 = gimple_call_arg (stmt, 0);
    2964        29511 :   tree arg2 = gimple_call_arg (stmt, 1);
    2965        29511 :   tree len = gimple_call_arg (stmt, 2);
    2966              : 
    2967              :   /* If the LEN parameter is zero, return zero.  */
    2968        29511 :   if (integer_zerop (len))
    2969              :     {
    2970            1 :       replace_call_with_value (gsi, build_int_cst (ptr_type_node, 0));
    2971            1 :       return true;
    2972              :     }
    2973              : 
    2974        29510 :   char c;
    2975        29510 :   if (TREE_CODE (arg2) != INTEGER_CST
    2976        16102 :       || !tree_fits_uhwi_p (len)
    2977        30238 :       || !target_char_cst_p (arg2, &c))
    2978              :     return false;
    2979              : 
    2980          728 :   unsigned HOST_WIDE_INT length = tree_to_uhwi (len);
    2981          728 :   unsigned HOST_WIDE_INT string_length;
    2982          728 :   const char *p1 = getbyterep (arg1, &string_length);
    2983              : 
    2984          728 :   if (p1)
    2985              :     {
    2986          110 :       const char *r = (const char *)memchr (p1, c, MIN (length, string_length));
    2987          110 :       if (r == NULL)
    2988              :         {
    2989           14 :           tree mem_size, offset_node;
    2990           14 :           byte_representation (arg1, &offset_node, &mem_size, NULL);
    2991           14 :           unsigned HOST_WIDE_INT offset = (offset_node == NULL_TREE)
    2992           14 :                                           ? 0 : tree_to_uhwi (offset_node);
    2993              :           /* MEM_SIZE is the size of the array the string literal
    2994              :              is stored in.  */
    2995           14 :           unsigned HOST_WIDE_INT string_size = tree_to_uhwi (mem_size) - offset;
    2996           14 :           gcc_checking_assert (string_length <= string_size);
    2997           14 :           if (length <= string_size)
    2998              :             {
    2999            4 :               replace_call_with_value (gsi, build_int_cst (ptr_type_node, 0));
    3000            4 :               return true;
    3001              :             }
    3002              :         }
    3003              :       else
    3004              :         {
    3005           96 :           unsigned HOST_WIDE_INT offset = r - p1;
    3006           96 :           gimple_seq stmts = NULL;
    3007           96 :           if (lhs != NULL_TREE)
    3008              :             {
    3009           94 :               tree offset_cst = build_int_cst (sizetype, offset);
    3010           94 :               gassign *stmt = gimple_build_assign (lhs, POINTER_PLUS_EXPR,
    3011              :                                                    arg1, offset_cst);
    3012           94 :               gimple_seq_add_stmt_without_update (&stmts, stmt);
    3013              :             }
    3014              :           else
    3015            2 :             gimple_seq_add_stmt_without_update (&stmts,
    3016              :                                                 gimple_build_nop ());
    3017              : 
    3018           96 :           gsi_replace_with_seq_vops (gsi, stmts);
    3019           96 :           return true;
    3020              :         }
    3021              :     }
    3022              : 
    3023              :   return false;
    3024              : }
    3025              : 
    3026              : /* Fold a call to the fputs builtin.  ARG0 and ARG1 are the arguments
    3027              :    to the call.  IGNORE is true if the value returned
    3028              :    by the builtin will be ignored.  UNLOCKED is true is true if this
    3029              :    actually a call to fputs_unlocked.  If LEN in non-NULL, it represents
    3030              :    the known length of the string.  Return NULL_TREE if no simplification
    3031              :    was possible.  */
    3032              : 
    3033              : static bool
    3034        20837 : gimple_fold_builtin_fputs (gimple_stmt_iterator *gsi,
    3035              :                            tree arg0, tree arg1,
    3036              :                            bool unlocked)
    3037              : {
    3038        20837 :   gimple *stmt = gsi_stmt (*gsi);
    3039              : 
    3040              :   /* If we're using an unlocked function, assume the other unlocked
    3041              :      functions exist explicitly.  */
    3042        20837 :   tree const fn_fputc = (unlocked
    3043        20837 :                          ? builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED)
    3044        20837 :                          : builtin_decl_implicit (BUILT_IN_FPUTC));
    3045        20794 :   tree const fn_fwrite = (unlocked
    3046           43 :                           ? builtin_decl_explicit (BUILT_IN_FWRITE_UNLOCKED)
    3047        20837 :                           : builtin_decl_implicit (BUILT_IN_FWRITE));
    3048              : 
    3049              :   /* If the return value is used, don't do the transformation.  */
    3050        20837 :   if (gimple_call_lhs (stmt))
    3051              :     return false;
    3052              : 
    3053              :   /* Get the length of the string passed to fputs.  If the length
    3054              :      can't be determined, punt.  */
    3055        20766 :   tree len = get_maxval_strlen (arg0, SRK_STRLEN);
    3056        20766 :   if (!len || TREE_CODE (len) != INTEGER_CST)
    3057              :     return false;
    3058              : 
    3059        16330 :   switch (compare_tree_int (len, 1))
    3060              :     {
    3061           91 :     case -1: /* length is 0, delete the call entirely .  */
    3062           91 :       replace_call_with_value (gsi, integer_zero_node);
    3063           91 :       return true;
    3064              : 
    3065         1064 :     case 0: /* length is 1, call fputc.  */
    3066         1064 :       {
    3067         1064 :         const char *p = c_getstr (arg0);
    3068         1064 :         if (p != NULL)
    3069              :           {
    3070         2100 :             if (!fn_fputc || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
    3071              :               return false;
    3072              : 
    3073         1050 :             gimple *repl
    3074         1050 :               = gimple_build_call (fn_fputc, 2,
    3075         1050 :                                    build_int_cst (integer_type_node, p[0]),
    3076              :                                    arg1);
    3077         1050 :             replace_call_with_call_and_fold (gsi, repl);
    3078         1050 :             return true;
    3079              :           }
    3080              :       }
    3081              :       /* FALLTHROUGH */
    3082        15189 :     case 1: /* length is greater than 1, call fwrite.  */
    3083        15189 :       {
    3084              :         /* If optimizing for size keep fputs.  */
    3085        15189 :         if (optimize_function_for_size_p (cfun))
    3086              :           return false;
    3087              :         /* New argument list transforming fputs(string, stream) to
    3088              :            fwrite(string, 1, len, stream).  */
    3089        16692 :         if (!fn_fwrite || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
    3090              :           return false;
    3091              : 
    3092         8346 :         gimple *repl
    3093         8346 :           = gimple_build_call (fn_fwrite, 4, arg0, size_one_node,
    3094              :                                fold_convert (size_type_node, len), arg1);
    3095         8346 :         replace_call_with_call_and_fold (gsi, repl);
    3096         8346 :         return true;
    3097              :       }
    3098            0 :     default:
    3099            0 :       gcc_unreachable ();
    3100              :     }
    3101              : }
    3102              : 
    3103              : /* Fold a call to fwrite (PTR, SIZE, N, STREAM) at *GSI.  UNLOCKED says whether
    3104              :    the callee is fwrite_unlocked rather than fwrite.  A call that transfers a
    3105              :    single byte and whose result is nobody's business writes the same byte as
    3106              :    fputc (*PTR, STREAM), which reaches the stream without going through the
    3107              :    generic buffered-write path.  Return true if the call was folded.  */
    3108              : 
    3109              : static bool
    3110        41395 : gimple_fold_builtin_fwrite (gimple_stmt_iterator *gsi, bool unlocked)
    3111              : {
    3112        41395 :   gimple *stmt = gsi_stmt (*gsi);
    3113              : 
    3114              :   /* fwrite reports the number of items transferred and fputc the character
    3115              :      written, so only fold when nothing looks at the result.  */
    3116        41395 :   if (gimple_call_lhs (stmt))
    3117              :     return false;
    3118              : 
    3119              :   /* fwrite transfers SIZE * N bytes, so writing a single byte needs both
    3120              :      counts to be one: no other pair of non-negative values multiplies to
    3121              :      one.  */
    3122        40247 :   if (!integer_onep (gimple_call_arg (stmt, 1))
    3123        40247 :       || !integer_onep (gimple_call_arg (stmt, 2)))
    3124              :     return false;
    3125              : 
    3126              :   /* If we're using an unlocked function, assume the other unlocked
    3127              :      functions exist explicitly.  */
    3128           13 :   tree const fn_fputc = (unlocked
    3129           13 :                          ? builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED)
    3130        41408 :                          : builtin_decl_implicit (BUILT_IN_FPUTC));
    3131           26 :   if (!fn_fputc || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
    3132              :     return false;
    3133              : 
    3134           13 :   location_t loc = gimple_location (stmt);
    3135           13 :   gimple_seq stmts = NULL;
    3136           13 :   tree byte = gimple_load_first_char (loc, gimple_call_arg (stmt, 0), &stmts);
    3137           13 :   tree c = gimple_convert (&stmts, integer_type_node, byte);
    3138           13 :   tree stream = gimple_call_arg (stmt, 3);
    3139           13 :   gcall *repl = gimple_build_call (fn_fputc, 2, c, stream);
    3140           13 :   gimple_seq_add_stmt_without_update (&stmts, repl);
    3141           13 :   gsi_replace_with_seq_vops (gsi, stmts);
    3142           13 :   return true;
    3143              : }
    3144              : 
    3145              : /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
    3146              :    DEST, SRC, LEN, and SIZE are the arguments to the call.
    3147              :    IGNORE is true, if return value can be ignored.  FCODE is the BUILT_IN_*
    3148              :    code of the builtin.  If MAXLEN is not NULL, it is maximum length
    3149              :    passed as third argument.  */
    3150              : 
    3151              : static bool
    3152        25701 : gimple_fold_builtin_memory_chk (gimple_stmt_iterator *gsi,
    3153              :                                 tree dest, tree src, tree len, tree size,
    3154              :                                 enum built_in_function fcode)
    3155              : {
    3156        25701 :   gimple *stmt = gsi_stmt (*gsi);
    3157        25701 :   location_t loc = gimple_location (stmt);
    3158        25701 :   bool ignore = gimple_call_lhs (stmt) == NULL_TREE;
    3159        25701 :   tree fn;
    3160              : 
    3161              :   /* If SRC and DEST are the same (and not volatile), return DEST
    3162              :      (resp. DEST+LEN for __mempcpy_chk).  */
    3163        25701 :   if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
    3164              :     {
    3165           13 :       if (fcode != BUILT_IN_MEMPCPY_CHK)
    3166              :         {
    3167            7 :           replace_call_with_value (gsi, dest);
    3168            7 :           return true;
    3169              :         }
    3170              :       else
    3171              :         {
    3172            6 :           gimple_seq stmts = NULL;
    3173            6 :           len = gimple_convert_to_ptrofftype (&stmts, loc, len);
    3174            6 :           tree temp = gimple_build (&stmts, loc, POINTER_PLUS_EXPR,
    3175            6 :                                     TREE_TYPE (dest), dest, len);
    3176            6 :           gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
    3177            6 :           replace_call_with_value (gsi, temp);
    3178            6 :           return true;
    3179              :         }
    3180              :     }
    3181              : 
    3182        51376 :   if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
    3183              :     return false;
    3184              : 
    3185        25688 :   tree maxlen = get_maxval_strlen (len, SRK_INT_VALUE);
    3186        25688 :   if (! integer_all_onesp (size)
    3187        24689 :       && !known_lower (stmt, len, size)
    3188        42927 :       && !known_lower (stmt, maxlen, size))
    3189              :     {
    3190              :       /* MAXLEN and LEN both cannot be proved to be less than SIZE, at
    3191              :          least try to optimize (void) __mempcpy_chk () into
    3192              :          (void) __memcpy_chk () */
    3193        17162 :       if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
    3194              :         {
    3195           43 :           fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
    3196           43 :           if (!fn)
    3197              :             return false;
    3198              : 
    3199           43 :           gimple *repl = gimple_build_call (fn, 4, dest, src, len, size);
    3200           43 :           replace_call_with_call_and_fold (gsi, repl);
    3201           43 :           return true;
    3202              :         }
    3203              :       return false;
    3204              :     }
    3205              : 
    3206         8526 :   fn = NULL_TREE;
    3207              :   /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
    3208              :      mem{cpy,pcpy,move,set} is available.  */
    3209         8526 :   switch (fcode)
    3210              :     {
    3211         1781 :     case BUILT_IN_MEMCPY_CHK:
    3212         1781 :       fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
    3213         1781 :       break;
    3214         1068 :     case BUILT_IN_MEMPCPY_CHK:
    3215         1068 :       fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
    3216         1068 :       break;
    3217         1652 :     case BUILT_IN_MEMMOVE_CHK:
    3218         1652 :       fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
    3219         1652 :       break;
    3220         4025 :     case BUILT_IN_MEMSET_CHK:
    3221         4025 :       fn = builtin_decl_explicit (BUILT_IN_MEMSET);
    3222         4025 :       break;
    3223              :     default:
    3224              :       break;
    3225              :     }
    3226              : 
    3227         8526 :   if (!fn)
    3228              :     return false;
    3229              : 
    3230         8526 :   gimple *repl = gimple_build_call (fn, 3, dest, src, len);
    3231         8526 :   replace_call_with_call_and_fold (gsi, repl);
    3232         8526 :   return true;
    3233              : }
    3234              : 
    3235              : /* Fold a call to the __st[rp]cpy_chk builtin.
    3236              :    DEST, SRC, and SIZE are the arguments to the call.
    3237              :    IGNORE is true if return value can be ignored.  FCODE is the BUILT_IN_*
    3238              :    code of the builtin.  If MAXLEN is not NULL, it is maximum length of
    3239              :    strings passed as second argument.  */
    3240              : 
    3241              : static bool
    3242         2566 : gimple_fold_builtin_stxcpy_chk (gimple_stmt_iterator *gsi,
    3243              :                                 tree dest,
    3244              :                                 tree src, tree size,
    3245              :                                 enum built_in_function fcode)
    3246              : {
    3247         2566 :   gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
    3248         2566 :   location_t loc = gimple_location (stmt);
    3249         2566 :   bool ignore = gimple_call_lhs (stmt) == NULL_TREE;
    3250         2566 :   tree len, fn;
    3251              : 
    3252              :   /* If SRC and DEST are the same (and not volatile), return DEST.  */
    3253         2566 :   if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
    3254              :     {
    3255              :       /* Issue -Wrestrict unless the pointers are null (those do
    3256              :          not point to objects and so do not indicate an overlap;
    3257              :          such calls could be the result of sanitization and jump
    3258              :          threading).  */
    3259            0 :       if (!integer_zerop (dest)
    3260            0 :           && !warning_suppressed_p (stmt, OPT_Wrestrict))
    3261              :         {
    3262            0 :           tree func = gimple_call_fndecl (stmt);
    3263              : 
    3264            0 :           warning_at (loc, OPT_Wrestrict,
    3265              :                       "%qD source argument is the same as destination",
    3266              :                       func);
    3267              :         }
    3268              : 
    3269            0 :       replace_call_with_value (gsi, dest);
    3270            0 :       return true;
    3271              :     }
    3272              : 
    3273         5132 :   if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
    3274              :     return false;
    3275              : 
    3276         2566 :   tree maxlen = get_maxval_strlen (src, SRK_STRLENMAX);
    3277         2566 :   if (! integer_all_onesp (size))
    3278              :     {
    3279         2497 :       len = c_strlen (src, 1);
    3280         2497 :       if (!known_lower (stmt, len, size, true)
    3281         2497 :           && !known_lower (stmt, maxlen, size, true))
    3282              :         {
    3283         2185 :           if (fcode == BUILT_IN_STPCPY_CHK)
    3284              :             {
    3285         1077 :               if (! ignore)
    3286              :                 return false;
    3287              : 
    3288              :               /* If return value of __stpcpy_chk is ignored,
    3289              :                  optimize into __strcpy_chk.  */
    3290           35 :               fn = builtin_decl_explicit (BUILT_IN_STRCPY_CHK);
    3291           35 :               if (!fn)
    3292              :                 return false;
    3293              : 
    3294           35 :               gimple *repl = gimple_build_call (fn, 3, dest, src, size);
    3295           35 :               replace_call_with_call_and_fold (gsi, repl);
    3296           35 :               return true;
    3297              :             }
    3298              : 
    3299         1108 :           if (! len || TREE_SIDE_EFFECTS (len))
    3300              :             return false;
    3301              : 
    3302              :           /* If c_strlen returned something, but not provably less than size,
    3303              :              transform __strcpy_chk into __memcpy_chk.  */
    3304          106 :           fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
    3305          106 :           if (!fn)
    3306              :             return false;
    3307              : 
    3308          106 :           gimple_seq stmts = NULL;
    3309          106 :           len = force_gimple_operand (len, &stmts, true, NULL_TREE);
    3310          106 :           len = gimple_convert (&stmts, loc, size_type_node, len);
    3311          106 :           len = gimple_build (&stmts, loc, PLUS_EXPR, size_type_node, len,
    3312              :                               build_int_cst (size_type_node, 1));
    3313          106 :           gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
    3314          106 :           gimple *repl = gimple_build_call (fn, 4, dest, src, len, size);
    3315          106 :           replace_call_with_call_and_fold (gsi, repl);
    3316          106 :           return true;
    3317              :         }
    3318              :     }
    3319              : 
    3320              :   /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available.  */
    3321          618 :   fn = builtin_decl_explicit (fcode == BUILT_IN_STPCPY_CHK && !ignore
    3322              :                               ? BUILT_IN_STPCPY : BUILT_IN_STRCPY);
    3323          381 :   if (!fn)
    3324              :     return false;
    3325              : 
    3326          381 :   gcall *repl = gimple_build_call (fn, 2, dest, src);
    3327          381 :   replace_call_with_call_and_fold (gsi, repl);
    3328          381 :   return true;
    3329              : }
    3330              : 
    3331              : /* Fold a call to the __st{r,p}ncpy_chk builtin.  DEST, SRC, LEN, and SIZE
    3332              :    are the arguments to the call.  If MAXLEN is not NULL, it is maximum
    3333              :    length passed as third argument. IGNORE is true if return value can be
    3334              :    ignored. FCODE is the BUILT_IN_* code of the builtin. */
    3335              : 
    3336              : static bool
    3337         2721 : gimple_fold_builtin_stxncpy_chk (gimple_stmt_iterator *gsi,
    3338              :                                  tree dest, tree src,
    3339              :                                  tree len, tree size,
    3340              :                                  enum built_in_function fcode)
    3341              : {
    3342         2721 :   gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
    3343         2721 :   bool ignore = gimple_call_lhs (stmt) == NULL_TREE;
    3344         2721 :   tree fn;
    3345              : 
    3346         2721 :   tree maxlen = get_maxval_strlen (len, SRK_INT_VALUE);
    3347         2721 :   if (! integer_all_onesp (size)
    3348         2721 :       && !known_lower (stmt, len, size) && !known_lower (stmt, maxlen, size))
    3349              :     {
    3350         2264 :       if (fcode == BUILT_IN_STPNCPY_CHK && ignore)
    3351              :         {
    3352              :           /* If return value of __stpncpy_chk is ignored,
    3353              :              optimize into __strncpy_chk.  */
    3354           39 :           fn = builtin_decl_explicit (BUILT_IN_STRNCPY_CHK);
    3355           39 :           if (fn)
    3356              :             {
    3357           39 :               gimple *repl = gimple_build_call (fn, 4, dest, src, len, size);
    3358           39 :               replace_call_with_call_and_fold (gsi, repl);
    3359           39 :               return true;
    3360              :             }
    3361              :         }
    3362              :       return false;
    3363              :     }
    3364              : 
    3365              :   /* If __builtin_st{r,p}ncpy_chk is used, assume st{r,p}ncpy is available.  */
    3366          717 :   fn = builtin_decl_explicit (fcode == BUILT_IN_STPNCPY_CHK && !ignore
    3367              :                               ? BUILT_IN_STPNCPY : BUILT_IN_STRNCPY);
    3368          914 :   if (!fn || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
    3369              :     return false;
    3370              : 
    3371          457 :   gcall *repl = gimple_build_call (fn, 3, dest, src, len);
    3372          457 :   replace_call_with_call_and_fold (gsi, repl);
    3373          457 :   return true;
    3374              : }
    3375              : 
    3376              : /* Fold function call to builtin stpcpy with arguments DEST and SRC.
    3377              :    Return NULL_TREE if no simplification can be made.  */
    3378              : 
    3379              : static bool
    3380         3674 : gimple_fold_builtin_stpcpy (gimple_stmt_iterator *gsi)
    3381              : {
    3382         3674 :   gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
    3383         3674 :   location_t loc = gimple_location (stmt);
    3384         3674 :   tree dest = gimple_call_arg (stmt, 0);
    3385         3674 :   tree src = gimple_call_arg (stmt, 1);
    3386         3674 :   tree fn, lenp1;
    3387              : 
    3388         7348 :   if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
    3389              :     return false;
    3390              : 
    3391              :   /* If the result is unused, replace stpcpy with strcpy.  */
    3392         3674 :   if (gimple_call_lhs (stmt) == NULL_TREE)
    3393              :     {
    3394           29 :       tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
    3395           29 :       if (!fn)
    3396              :         return false;
    3397           29 :       gimple_call_set_fndecl (stmt, fn);
    3398           29 :       fold_stmt (gsi);
    3399           29 :       return true;
    3400              :     }
    3401              : 
    3402              :   /* Set to non-null if ARG refers to an unterminated array.  */
    3403         3645 :   c_strlen_data data = { };
    3404              :   /* The size of the unterminated array if SRC refers to one.  */
    3405         3645 :   tree size;
    3406              :   /* True if the size is exact/constant, false if it's the lower bound
    3407              :      of a range.  */
    3408         3645 :   bool exact;
    3409         3645 :   tree len = c_strlen (src, 1, &data, 1);
    3410         3645 :   if (!len
    3411          703 :       || TREE_CODE (len) != INTEGER_CST)
    3412              :     {
    3413         3174 :       data.decl = unterminated_array (src, &size, &exact);
    3414         3174 :       if (!data.decl)
    3415              :         return false;
    3416              :     }
    3417              : 
    3418         1076 :   if (data.decl)
    3419              :     {
    3420              :       /* Avoid folding calls with unterminated arrays.  */
    3421          605 :       if (!warning_suppressed_p (stmt, OPT_Wstringop_overread))
    3422           75 :         warn_string_no_nul (loc, stmt, "stpcpy", src, data.decl, size,
    3423              :                             exact);
    3424          605 :       suppress_warning (stmt, OPT_Wstringop_overread);
    3425          605 :       return false;
    3426              :     }
    3427              : 
    3428          471 :   if (optimize_function_for_size_p (cfun)
    3429              :       /* If length is zero it's small enough.  */
    3430          471 :       && !integer_zerop (len))
    3431              :     return false;
    3432              : 
    3433              :   /* If the source has a known length replace stpcpy with memcpy.  */
    3434          287 :   fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
    3435          287 :   if (!fn)
    3436              :     return false;
    3437              : 
    3438          287 :   gimple_seq stmts = NULL;
    3439          287 :   tree tem = gimple_convert (&stmts, loc, size_type_node, len);
    3440          287 :   lenp1 = gimple_build (&stmts, loc, PLUS_EXPR, size_type_node,
    3441              :                         tem, build_int_cst (size_type_node, 1));
    3442          287 :   gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
    3443          287 :   gcall *repl = gimple_build_call (fn, 3, dest, src, lenp1);
    3444          287 :   gimple_move_vops (repl, stmt);
    3445          287 :   gsi_insert_before (gsi, repl, GSI_SAME_STMT);
    3446              :   /* Replace the result with dest + len.  */
    3447          287 :   stmts = NULL;
    3448          287 :   tem = gimple_convert (&stmts, loc, sizetype, len);
    3449          287 :   gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
    3450          287 :   gassign *ret = gimple_build_assign (gimple_call_lhs (stmt),
    3451              :                                       POINTER_PLUS_EXPR, dest, tem);
    3452          287 :   gsi_replace (gsi, ret, false);
    3453              :   /* Finally fold the memcpy call.  */
    3454          287 :   gimple_stmt_iterator gsi2 = *gsi;
    3455          287 :   gsi_prev (&gsi2);
    3456          287 :   fold_stmt (&gsi2);
    3457          287 :   return true;
    3458              : }
    3459              : 
    3460              : /* Simplify mempcpy call stmt at GSI, returning true if simplified.
    3461              :    Currently only handling mempcpy -> memcpy when the return value
    3462              :    is ignored.  */
    3463              : 
    3464              : static bool
    3465         9545 : gimple_fold_builtin_mempcpy (gimple_stmt_iterator *gsi)
    3466              : {
    3467         9545 :   gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
    3468              : 
    3469         9545 :   if (gimple_call_lhs (stmt) != NULL_TREE)
    3470              :     return false;
    3471              : 
    3472          385 :   tree fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
    3473          385 :   if (!fn)
    3474              :     return false;
    3475              : 
    3476          385 :   tree dest = gimple_call_arg (stmt, 0);
    3477          385 :   tree src = gimple_call_arg (stmt, 1);
    3478          385 :   tree n = gimple_call_arg (stmt, 2);
    3479              : 
    3480          385 :   gcall *repl = gimple_build_call (fn, 3, dest, src, n);
    3481          385 :   replace_call_with_call_and_fold (gsi, repl);
    3482              : 
    3483          385 :   return true;
    3484              : }
    3485              : 
    3486              : /* Fold a call EXP to {,v}snprintf having NARGS passed as ARGS.  Return
    3487              :    NULL_TREE if a normal call should be emitted rather than expanding
    3488              :    the function inline.  FCODE is either BUILT_IN_SNPRINTF_CHK or
    3489              :    BUILT_IN_VSNPRINTF_CHK.  If MAXLEN is not NULL, it is maximum length
    3490              :    passed as second argument.  */
    3491              : 
    3492              : static bool
    3493         2359 : gimple_fold_builtin_snprintf_chk (gimple_stmt_iterator *gsi,
    3494              :                                   enum built_in_function fcode)
    3495              : {
    3496         2359 :   gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
    3497         2359 :   tree dest, size, len, fn, fmt, flag;
    3498         2359 :   const char *fmt_str;
    3499              : 
    3500              :   /* Verify the required arguments in the original call.  */
    3501         2359 :   if (gimple_call_num_args (stmt) < 5)
    3502              :     return false;
    3503              : 
    3504         2359 :   dest = gimple_call_arg (stmt, 0);
    3505         2359 :   len = gimple_call_arg (stmt, 1);
    3506         2359 :   flag = gimple_call_arg (stmt, 2);
    3507         2359 :   size = gimple_call_arg (stmt, 3);
    3508         2359 :   fmt = gimple_call_arg (stmt, 4);
    3509              : 
    3510         2359 :   tree maxlen = get_maxval_strlen (len, SRK_INT_VALUE);
    3511         2359 :   if (! integer_all_onesp (size)
    3512         2359 :       && !known_lower (stmt, len, size) && !known_lower (stmt, maxlen, size))
    3513              :     return false;
    3514              : 
    3515          308 :   if (!init_target_chars ())
    3516              :     return false;
    3517              : 
    3518              :   /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
    3519              :      or if format doesn't contain % chars or is "%s".  */
    3520          308 :   if (! integer_zerop (flag))
    3521              :     {
    3522           52 :       fmt_str = c_getstr (fmt);
    3523           52 :       if (fmt_str == NULL)
    3524              :         return false;
    3525           52 :       if (strchr (fmt_str, target_percent) != NULL
    3526           51 :           && strcmp (fmt_str, target_percent_s))
    3527              :         return false;
    3528              :     }
    3529              : 
    3530              :   /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
    3531              :      available.  */
    3532          415 :   fn = builtin_decl_explicit (fcode == BUILT_IN_VSNPRINTF_CHK
    3533              :                               ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF);
    3534          518 :   if (!fn || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
    3535              :     return false;
    3536              : 
    3537              :   /* Replace the called function and the first 5 argument by 3 retaining
    3538              :      trailing varargs.  */
    3539          259 :   gimple_call_set_fndecl (stmt, fn);
    3540          259 :   gimple_call_set_fntype (stmt, TREE_TYPE (fn));
    3541          259 :   gimple_call_set_arg (stmt, 0, dest);
    3542          259 :   gimple_call_set_arg (stmt, 1, len);
    3543          259 :   gimple_call_set_arg (stmt, 2, fmt);
    3544          546 :   for (unsigned i = 3; i < gimple_call_num_args (stmt) - 2; ++i)
    3545          287 :     gimple_call_set_arg (stmt, i, gimple_call_arg (stmt, i + 2));
    3546          259 :   gimple_set_num_ops (stmt, gimple_num_ops (stmt) - 2);
    3547          259 :   fold_stmt (gsi);
    3548          259 :   return true;
    3549              : }
    3550              : 
    3551              : /* Fold a call EXP to __{,v}sprintf_chk having NARGS passed as ARGS.
    3552              :    Return NULL_TREE if a normal call should be emitted rather than
    3553              :    expanding the function inline.  FCODE is either BUILT_IN_SPRINTF_CHK
    3554              :    or BUILT_IN_VSPRINTF_CHK.  */
    3555              : 
    3556              : static bool
    3557         4471 : gimple_fold_builtin_sprintf_chk (gimple_stmt_iterator *gsi,
    3558              :                                  enum built_in_function fcode)
    3559              : {
    3560         4471 :   gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
    3561         4471 :   tree dest, size, len, fn, fmt, flag;
    3562         4471 :   const char *fmt_str;
    3563         4471 :   unsigned nargs = gimple_call_num_args (stmt);
    3564              : 
    3565              :   /* Verify the required arguments in the original call.  */
    3566         4471 :   if (nargs < 4)
    3567              :     return false;
    3568         4471 :   dest = gimple_call_arg (stmt, 0);
    3569         4471 :   flag = gimple_call_arg (stmt, 1);
    3570         4471 :   size = gimple_call_arg (stmt, 2);
    3571         4471 :   fmt = gimple_call_arg (stmt, 3);
    3572              : 
    3573         4471 :   len = NULL_TREE;
    3574              : 
    3575         4471 :   if (!init_target_chars ())
    3576              :     return false;
    3577              : 
    3578              :   /* Check whether the format is a literal string constant.  */
    3579         4471 :   fmt_str = c_getstr (fmt);
    3580         4471 :   if (fmt_str != NULL)
    3581              :     {
    3582              :       /* If the format doesn't contain % args or %%, we know the size.  */
    3583         4078 :       if (strchr (fmt_str, target_percent) == 0)
    3584              :         {
    3585          251 :           if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4)
    3586          251 :             len = build_int_cstu (size_type_node, strlen (fmt_str));
    3587              :         }
    3588              :       /* If the format is "%s" and first ... argument is a string literal,
    3589              :          we know the size too.  */
    3590         3827 :       else if (fcode == BUILT_IN_SPRINTF_CHK
    3591         2971 :                && strcmp (fmt_str, target_percent_s) == 0)
    3592              :         {
    3593          395 :           tree arg;
    3594              : 
    3595          395 :           if (nargs == 5)
    3596              :             {
    3597          395 :               arg = gimple_call_arg (stmt, 4);
    3598          395 :               if (POINTER_TYPE_P (TREE_TYPE (arg)))
    3599          363 :                 len = c_strlen (arg, 1);
    3600              :             }
    3601              :         }
    3602              :     }
    3603              : 
    3604         4471 :   if (! integer_all_onesp (size) && !known_lower (stmt, len, size, true))
    3605              :     return false;
    3606              : 
    3607              :   /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
    3608              :      or if format doesn't contain % chars or is "%s".  */
    3609          202 :   if (! integer_zerop (flag))
    3610              :     {
    3611            1 :       if (fmt_str == NULL)
    3612              :         return false;
    3613            1 :       if (strchr (fmt_str, target_percent) != NULL
    3614            0 :           && strcmp (fmt_str, target_percent_s))
    3615              :         return false;
    3616              :     }
    3617              : 
    3618              :   /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available.  */
    3619          347 :   fn = builtin_decl_explicit (fcode == BUILT_IN_VSPRINTF_CHK
    3620              :                               ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF);
    3621          404 :   if (!fn || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
    3622              :     return false;
    3623              : 
    3624              :   /* Replace the called function and the first 4 argument by 2 retaining
    3625              :      trailing varargs.  */
    3626          202 :   gimple_call_set_fndecl (stmt, fn);
    3627          202 :   gimple_call_set_fntype (stmt, TREE_TYPE (fn));
    3628          202 :   gimple_call_set_arg (stmt, 0, dest);
    3629          202 :   gimple_call_set_arg (stmt, 1, fmt);
    3630          400 :   for (unsigned i = 2; i < gimple_call_num_args (stmt) - 2; ++i)
    3631          198 :     gimple_call_set_arg (stmt, i, gimple_call_arg (stmt, i + 2));
    3632          202 :   gimple_set_num_ops (stmt, gimple_num_ops (stmt) - 2);
    3633          202 :   fold_stmt (gsi);
    3634          202 :   return true;
    3635              : }
    3636              : 
    3637              : /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
    3638              :    ORIG may be null if this is a 2-argument call.  We don't attempt to
    3639              :    simplify calls with more than 3 arguments.
    3640              : 
    3641              :    Return true if simplification was possible, otherwise false.  */
    3642              : 
    3643              : bool
    3644         2348 : gimple_fold_builtin_sprintf (gimple_stmt_iterator *gsi)
    3645              : {
    3646         2348 :   gimple *stmt = gsi_stmt (*gsi);
    3647              : 
    3648              :   /* Verify the required arguments in the original call.  We deal with two
    3649              :      types of sprintf() calls: 'sprintf (str, fmt)' and
    3650              :      'sprintf (dest, "%s", orig)'.  */
    3651         2348 :   if (gimple_call_num_args (stmt) > 3)
    3652              :     return false;
    3653              : 
    3654         1943 :   tree orig = NULL_TREE;
    3655         1943 :   if (gimple_call_num_args (stmt) == 3)
    3656         1846 :     orig = gimple_call_arg (stmt, 2);
    3657              : 
    3658              :   /* Check whether the format is a literal string constant.  */
    3659         1943 :   tree fmt = gimple_call_arg (stmt, 1);
    3660         1943 :   const char *fmt_str = c_getstr (fmt);
    3661         1943 :   if (fmt_str == NULL)
    3662              :     return false;
    3663              : 
    3664         1943 :   tree dest = gimple_call_arg (stmt, 0);
    3665              : 
    3666         1943 :   if (!init_target_chars ())
    3667              :     return false;
    3668              : 
    3669         1943 :   tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
    3670         3886 :   if (!fn || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
    3671              :     return false;
    3672              : 
    3673              :   /* If the format doesn't contain % args or %%, use strcpy.  */
    3674         1943 :   if (strchr (fmt_str, target_percent) == NULL)
    3675              :     {
    3676              :       /* Don't optimize sprintf (buf, "abc", ptr++).  */
    3677          109 :       if (orig)
    3678              :         return false;
    3679              : 
    3680              :       /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
    3681              :          'format' is known to contain no % formats.  */
    3682           96 :       gimple_seq stmts = NULL;
    3683           96 :       gimple *repl = gimple_build_call (fn, 2, dest, fmt);
    3684              : 
    3685              :       /* Propagate the NO_WARNING bit to avoid issuing the same
    3686              :          warning more than once.  */
    3687           96 :       copy_warning (repl, stmt);
    3688              : 
    3689           96 :       gimple_seq_add_stmt_without_update (&stmts, repl);
    3690           96 :       if (tree lhs = gimple_call_lhs (stmt))
    3691              :         {
    3692            0 :           repl = gimple_build_assign (lhs, build_int_cst (TREE_TYPE (lhs),
    3693            0 :                                                           strlen (fmt_str)));
    3694            0 :           gimple_seq_add_stmt_without_update (&stmts, repl);
    3695            0 :           gsi_replace_with_seq_vops (gsi, stmts);
    3696              :           /* gsi now points at the assignment to the lhs, get a
    3697              :              stmt iterator to the memcpy call.
    3698              :              ???  We can't use gsi_for_stmt as that doesn't work when the
    3699              :              CFG isn't built yet.  */
    3700            0 :           gimple_stmt_iterator gsi2 = *gsi;
    3701            0 :           gsi_prev (&gsi2);
    3702            0 :           fold_stmt (&gsi2);
    3703              :         }
    3704              :       else
    3705              :         {
    3706           96 :           gsi_replace_with_seq_vops (gsi, stmts);
    3707           96 :           fold_stmt (gsi);
    3708              :         }
    3709              :       return true;
    3710              :     }
    3711              : 
    3712              :   /* If the format is "%s", use strcpy if the result isn't used.  */
    3713         1834 :   else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
    3714              :     {
    3715              :       /* Don't crash on sprintf (str1, "%s").  */
    3716          746 :       if (!orig)
    3717              :         return false;
    3718              : 
    3719              :       /* Don't fold calls with source arguments of invalid (nonpointer)
    3720              :          types.  */
    3721          745 :       if (!POINTER_TYPE_P (TREE_TYPE (orig)))
    3722              :         return false;
    3723              : 
    3724          739 :       tree orig_len = NULL_TREE;
    3725          739 :       if (gimple_call_lhs (stmt))
    3726              :         {
    3727           17 :           orig_len = get_maxval_strlen (orig, SRK_STRLEN);
    3728           17 :           if (!orig_len)
    3729              :             return false;
    3730              :         }
    3731              : 
    3732              :       /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2).  */
    3733          722 :       gimple_seq stmts = NULL;
    3734          722 :       gimple *repl = gimple_build_call (fn, 2, dest, orig);
    3735              : 
    3736              :       /* Propagate the NO_WARNING bit to avoid issuing the same
    3737              :          warning more than once.  */
    3738          722 :       copy_warning (repl, stmt);
    3739              : 
    3740          722 :       gimple_seq_add_stmt_without_update (&stmts, repl);
    3741          722 :       if (tree lhs = gimple_call_lhs (stmt))
    3742              :         {
    3743            0 :           if (!useless_type_conversion_p (TREE_TYPE (lhs),
    3744            0 :                                           TREE_TYPE (orig_len)))
    3745            0 :             orig_len = fold_convert (TREE_TYPE (lhs), orig_len);
    3746            0 :           repl = gimple_build_assign (lhs, orig_len);
    3747            0 :           gimple_seq_add_stmt_without_update (&stmts, repl);
    3748            0 :           gsi_replace_with_seq_vops (gsi, stmts);
    3749              :           /* gsi now points at the assignment to the lhs, get a
    3750              :              stmt iterator to the memcpy call.
    3751              :              ???  We can't use gsi_for_stmt as that doesn't work when the
    3752              :              CFG isn't built yet.  */
    3753            0 :           gimple_stmt_iterator gsi2 = *gsi;
    3754            0 :           gsi_prev (&gsi2);
    3755            0 :           fold_stmt (&gsi2);
    3756              :         }
    3757              :       else
    3758              :         {
    3759          722 :           gsi_replace_with_seq_vops (gsi, stmts);
    3760          722 :           fold_stmt (gsi);
    3761              :         }
    3762              :       return true;
    3763              :     }
    3764              :   return false;
    3765              : }
    3766              : 
    3767              : /* Simplify a call to the snprintf builtin with arguments DEST, DESTSIZE,
    3768              :    FMT, and ORIG.  ORIG may be null if this is a 3-argument call.  We don't
    3769              :    attempt to simplify calls with more than 4 arguments.
    3770              : 
    3771              :    Return true if simplification was possible, otherwise false.  */
    3772              : 
    3773              : bool
    3774         1878 : gimple_fold_builtin_snprintf (gimple_stmt_iterator *gsi)
    3775              : {
    3776         1878 :   gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
    3777         1878 :   tree dest = gimple_call_arg (stmt, 0);
    3778         1878 :   tree destsize = gimple_call_arg (stmt, 1);
    3779         1878 :   tree fmt = gimple_call_arg (stmt, 2);
    3780         1878 :   tree orig = NULL_TREE;
    3781         1878 :   const char *fmt_str = NULL;
    3782              : 
    3783         1878 :   if (gimple_call_num_args (stmt) > 4
    3784         2784 :       || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
    3785              :     return false;
    3786              : 
    3787          903 :   if (gimple_call_num_args (stmt) == 4)
    3788          704 :     orig = gimple_call_arg (stmt, 3);
    3789              : 
    3790              :   /* Check whether the format is a literal string constant.  */
    3791          903 :   fmt_str = c_getstr (fmt);
    3792          903 :   if (fmt_str == NULL)
    3793              :     return false;
    3794              : 
    3795          903 :   if (!init_target_chars ())
    3796              :     return false;
    3797              : 
    3798              :   /* If the format doesn't contain % args or %%, use strcpy.  */
    3799          903 :   if (strchr (fmt_str, target_percent) == NULL)
    3800              :     {
    3801          396 :       tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
    3802          198 :       if (!fn)
    3803              :         return false;
    3804              : 
    3805              :       /* Don't optimize snprintf (buf, 4, "abc", ptr++).  */
    3806          198 :       if (orig)
    3807              :         return false;
    3808              : 
    3809          198 :       tree len = build_int_cstu (TREE_TYPE (destsize), strlen (fmt_str));
    3810              : 
    3811              :       /* We could expand this as
    3812              :          memcpy (str, fmt, cst - 1); str[cst - 1] = '\0';
    3813              :          or to
    3814              :          memcpy (str, fmt_with_nul_at_cstm1, cst);
    3815              :          but in the former case that might increase code size
    3816              :          and in the latter case grow .rodata section too much.
    3817              :          So punt for now.  */
    3818          198 :       if (!known_lower (stmt, len, destsize, true))
    3819              :         return false;
    3820              : 
    3821          168 :       gimple_seq stmts = NULL;
    3822          168 :       gimple *repl = gimple_build_call (fn, 2, dest, fmt);
    3823          168 :       gimple_seq_add_stmt_without_update (&stmts, repl);
    3824          168 :       if (tree lhs = gimple_call_lhs (stmt))
    3825              :         {
    3826            0 :           repl = gimple_build_assign (lhs,
    3827            0 :                                       fold_convert (TREE_TYPE (lhs), len));
    3828            0 :           gimple_seq_add_stmt_without_update (&stmts, repl);
    3829            0 :           gsi_replace_with_seq_vops (gsi, stmts);
    3830              :           /* gsi now points at the assignment to the lhs, get a
    3831              :              stmt iterator to the memcpy call.
    3832              :              ???  We can't use gsi_for_stmt as that doesn't work when the
    3833              :              CFG isn't built yet.  */
    3834            0 :           gimple_stmt_iterator gsi2 = *gsi;
    3835            0 :           gsi_prev (&gsi2);
    3836            0 :           fold_stmt (&gsi2);
    3837              :         }
    3838              :       else
    3839              :         {
    3840          168 :           gsi_replace_with_seq_vops (gsi, stmts);
    3841          168 :           fold_stmt (gsi);
    3842              :         }
    3843              :       return true;
    3844              :     }
    3845              : 
    3846              :   /* If the format is "%s", use strcpy if the result isn't used.  */
    3847          705 :   else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
    3848              :     {
    3849          516 :       tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
    3850          258 :       if (!fn)
    3851              :         return false;
    3852              : 
    3853              :       /* Don't crash on snprintf (str1, cst, "%s").  */
    3854          258 :       if (!orig)
    3855              :         return false;
    3856              : 
    3857          258 :       tree orig_len = get_maxval_strlen (orig, SRK_STRLEN);
    3858              : 
    3859              :       /* We could expand this as
    3860              :          memcpy (str1, str2, cst - 1); str1[cst - 1] = '\0';
    3861              :          or to
    3862              :          memcpy (str1, str2_with_nul_at_cstm1, cst);
    3863              :          but in the former case that might increase code size
    3864              :          and in the latter case grow .rodata section too much.
    3865              :          So punt for now.  */
    3866          258 :       if (!known_lower (stmt, orig_len, destsize, true))
    3867              :         return false;
    3868              : 
    3869              :       /* Convert snprintf (str1, cst, "%s", str2) into
    3870              :          strcpy (str1, str2) if strlen (str2) < cst.  */
    3871           64 :       gimple_seq stmts = NULL;
    3872           64 :       gimple *repl = gimple_build_call (fn, 2, dest, orig);
    3873           64 :       gimple_seq_add_stmt_without_update (&stmts, repl);
    3874           64 :       if (tree lhs = gimple_call_lhs (stmt))
    3875              :         {
    3876            0 :           if (!useless_type_conversion_p (TREE_TYPE (lhs),
    3877            0 :                                           TREE_TYPE (orig_len)))
    3878            0 :             orig_len = fold_convert (TREE_TYPE (lhs), orig_len);
    3879            0 :           repl = gimple_build_assign (lhs, orig_len);
    3880            0 :           gimple_seq_add_stmt_without_update (&stmts, repl);
    3881            0 :           gsi_replace_with_seq_vops (gsi, stmts);
    3882              :           /* gsi now points at the assignment to the lhs, get a
    3883              :              stmt iterator to the memcpy call.
    3884              :              ???  We can't use gsi_for_stmt as that doesn't work when the
    3885              :              CFG isn't built yet.  */
    3886            0 :           gimple_stmt_iterator gsi2 = *gsi;
    3887            0 :           gsi_prev (&gsi2);
    3888            0 :           fold_stmt (&gsi2);
    3889              :         }
    3890              :       else
    3891              :         {
    3892           64 :           gsi_replace_with_seq_vops (gsi, stmts);
    3893           64 :           fold_stmt (gsi);
    3894              :         }
    3895              :       return true;
    3896              :     }
    3897              :   return false;
    3898              : }
    3899              : 
    3900              : /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
    3901              :    FP, FMT, and ARG are the arguments to the call.  We don't fold calls with
    3902              :    more than 3 arguments, and ARG may be null in the 2-argument case.
    3903              : 
    3904              :    Return NULL_TREE if no simplification was possible, otherwise return the
    3905              :    simplified form of the call as a tree.  FCODE is the BUILT_IN_*
    3906              :    code of the function to be simplified.  */
    3907              : 
    3908              : static bool
    3909        54739 : gimple_fold_builtin_fprintf (gimple_stmt_iterator *gsi,
    3910              :                              tree fp, tree fmt, tree arg,
    3911              :                              enum built_in_function fcode)
    3912              : {
    3913        54739 :   gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
    3914        54739 :   tree fn_fputc, fn_fputs;
    3915        54739 :   const char *fmt_str = NULL;
    3916              : 
    3917              :   /* If the return value is used, don't do the transformation.  */
    3918        54739 :   if (gimple_call_lhs (stmt) != NULL_TREE)
    3919              :     return false;
    3920              : 
    3921              :   /* Check whether the format is a literal string constant.  */
    3922        50562 :   fmt_str = c_getstr (fmt);
    3923        50562 :   if (fmt_str == NULL)
    3924              :     return false;
    3925              : 
    3926        50232 :   if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
    3927              :     {
    3928              :       /* If we're using an unlocked function, assume the other
    3929              :          unlocked functions exist explicitly.  */
    3930           80 :       fn_fputc = builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED);
    3931           80 :       fn_fputs = builtin_decl_explicit (BUILT_IN_FPUTS_UNLOCKED);
    3932              :     }
    3933              :   else
    3934              :     {
    3935        50152 :       fn_fputc = builtin_decl_implicit (BUILT_IN_FPUTC);
    3936        50152 :       fn_fputs = builtin_decl_implicit (BUILT_IN_FPUTS);
    3937              :     }
    3938              : 
    3939        50232 :   if (!init_target_chars ())
    3940              :     return false;
    3941              : 
    3942       100464 :   if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
    3943              :     return false;
    3944              : 
    3945              :   /* If the format doesn't contain % args or %%, use strcpy.  */
    3946        50232 :   if (strchr (fmt_str, target_percent) == NULL)
    3947              :     {
    3948         9703 :       if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
    3949         9633 :           && arg)
    3950              :         return false;
    3951              : 
    3952              :       /* If the format specifier was "", fprintf does nothing.  */
    3953         9703 :       if (fmt_str[0] == '\0')
    3954              :         {
    3955           58 :           replace_call_with_value (gsi, NULL_TREE);
    3956           58 :           return true;
    3957              :         }
    3958              : 
    3959              :       /* When "string" doesn't contain %, replace all cases of
    3960              :          fprintf (fp, string) with fputs (string, fp).  The fputs
    3961              :          builtin will take care of special cases like length == 1.  */
    3962         9645 :       if (fn_fputs)
    3963              :         {
    3964         9645 :           gcall *repl = gimple_build_call (fn_fputs, 2, fmt, fp);
    3965         9645 :           replace_call_with_call_and_fold (gsi, repl);
    3966         9645 :           return true;
    3967              :         }
    3968              :     }
    3969              : 
    3970              :   /* The other optimizations can be done only on the non-va_list variants.  */
    3971        40529 :   else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
    3972              :     return false;
    3973              : 
    3974              :   /* If the format specifier was "%s", call __builtin_fputs (arg, fp).  */
    3975        39480 :   else if (strcmp (fmt_str, target_percent_s) == 0)
    3976              :     {
    3977          643 :       if (!arg || ! POINTER_TYPE_P (TREE_TYPE (arg)))
    3978              :         return false;
    3979          643 :       if (fn_fputs)
    3980              :         {
    3981          643 :           gcall *repl = gimple_build_call (fn_fputs, 2, arg, fp);
    3982          643 :           replace_call_with_call_and_fold (gsi, repl);
    3983          643 :           return true;
    3984              :         }
    3985              :     }
    3986              : 
    3987              :   /* If the format specifier was "%c", call __builtin_fputc (arg, fp).  */
    3988        38837 :   else if (strcmp (fmt_str, target_percent_c) == 0)
    3989              :     {
    3990           49 :       if (!arg
    3991           49 :           || ! useless_type_conversion_p (integer_type_node, TREE_TYPE (arg)))
    3992              :         return false;
    3993           49 :       if (fn_fputc)
    3994              :         {
    3995           49 :           gcall *repl = gimple_build_call (fn_fputc, 2, arg, fp);
    3996           49 :           replace_call_with_call_and_fold (gsi, repl);
    3997           49 :           return true;
    3998              :         }
    3999              :     }
    4000              : 
    4001              :   return false;
    4002              : }
    4003              : 
    4004              : /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
    4005              :    FMT and ARG are the arguments to the call; we don't fold cases with
    4006              :    more than 2 arguments, and ARG may be null if this is a 1-argument case.
    4007              : 
    4008              :    Return NULL_TREE if no simplification was possible, otherwise return the
    4009              :    simplified form of the call as a tree.  FCODE is the BUILT_IN_*
    4010              :    code of the function to be simplified.  */
    4011              : 
    4012              : static bool
    4013       126068 : gimple_fold_builtin_printf (gimple_stmt_iterator *gsi, tree fmt,
    4014              :                             tree arg, enum built_in_function fcode)
    4015              : {
    4016       126068 :   gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
    4017       126068 :   tree fn_putchar, fn_puts, newarg;
    4018       126068 :   const char *fmt_str = NULL;
    4019              : 
    4020              :   /* If the return value is used, don't do the transformation.  */
    4021       126068 :   if (gimple_call_lhs (stmt) != NULL_TREE)
    4022              :     return false;
    4023              : 
    4024       245620 :   if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
    4025              :     return false;
    4026              : 
    4027              :   /* Check whether the format is a literal string constant.  */
    4028       122810 :   fmt_str = c_getstr (fmt);
    4029       122810 :   if (fmt_str == NULL)
    4030              :     return false;
    4031              : 
    4032       119773 :   if (fcode == BUILT_IN_PRINTF_UNLOCKED)
    4033              :     {
    4034              :       /* If we're using an unlocked function, assume the other
    4035              :          unlocked functions exist explicitly.  */
    4036           80 :       fn_putchar = builtin_decl_explicit (BUILT_IN_PUTCHAR_UNLOCKED);
    4037           80 :       fn_puts = builtin_decl_explicit (BUILT_IN_PUTS_UNLOCKED);
    4038              :     }
    4039              :   else
    4040              :     {
    4041       119693 :       fn_putchar = builtin_decl_implicit (BUILT_IN_PUTCHAR);
    4042       119693 :       fn_puts = builtin_decl_implicit (BUILT_IN_PUTS);
    4043              :     }
    4044              : 
    4045       119773 :   if (!init_target_chars ())
    4046              :     return false;
    4047              : 
    4048       119773 :   if (strcmp (fmt_str, target_percent_s) == 0
    4049       113387 :       || strchr (fmt_str, target_percent) == NULL)
    4050              :     {
    4051        14235 :       const char *str;
    4052              : 
    4053        14235 :       if (strcmp (fmt_str, target_percent_s) == 0)
    4054              :         {
    4055         6386 :           if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
    4056              :             return false;
    4057              : 
    4058         6098 :           if (!arg || ! POINTER_TYPE_P (TREE_TYPE (arg)))
    4059              :             return false;
    4060              : 
    4061         6093 :           str = c_getstr (arg);
    4062         6093 :           if (str == NULL)
    4063              :             return false;
    4064              :         }
    4065              :       else
    4066              :         {
    4067              :           /* The format specifier doesn't contain any '%' characters.  */
    4068         7849 :           if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
    4069         7717 :               && arg)
    4070              :             return false;
    4071              :           str = fmt_str;
    4072              :         }
    4073              : 
    4074              :       /* If the string was "", printf does nothing.  */
    4075         5800 :       if (str[0] == '\0')
    4076              :         {
    4077          109 :           replace_call_with_value (gsi, NULL_TREE);
    4078          109 :           return true;
    4079              :         }
    4080              : 
    4081              :       /* If the string has length of 1, call putchar.  */
    4082         5691 :       if (str[1] == '\0')
    4083              :         {
    4084              :           /* Given printf("c"), (where c is any one character,)
    4085              :              convert "c"[0] to an int and pass that to the replacement
    4086              :              function.  */
    4087          559 :           newarg = build_int_cst (integer_type_node, str[0]);
    4088          559 :           if (fn_putchar)
    4089              :             {
    4090          559 :               gcall *repl = gimple_build_call (fn_putchar, 1, newarg);
    4091          559 :               replace_call_with_call_and_fold (gsi, repl);
    4092          559 :               return true;
    4093              :             }
    4094              :         }
    4095              :       else
    4096              :         {
    4097              :           /* If the string was "string\n", call puts("string").  */
    4098         5132 :           size_t len = strlen (str);
    4099         5132 :           if ((unsigned char)str[len - 1] == target_newline
    4100         4032 :               && (size_t) (int) len == len
    4101         4032 :               && (int) len > 0)
    4102              :             {
    4103         4032 :               char *newstr;
    4104              : 
    4105              :               /* Create a NUL-terminated string that's one char shorter
    4106              :                  than the original, stripping off the trailing '\n'.  */
    4107         4032 :               newstr = xstrdup (str);
    4108         4032 :               newstr[len - 1] = '\0';
    4109         4032 :               newarg = build_string_literal (len, newstr);
    4110         4032 :               free (newstr);
    4111         4032 :               if (fn_puts)
    4112              :                 {
    4113         4032 :                   gcall *repl = gimple_build_call (fn_puts, 1, newarg);
    4114         4032 :                   replace_call_with_call_and_fold (gsi, repl);
    4115         4032 :                   return true;
    4116              :                 }
    4117              :             }
    4118              :           else
    4119              :             /* We'd like to arrange to call fputs(string,stdout) here,
    4120              :                but we need stdout and don't have a way to get it yet.  */
    4121              :             return false;
    4122              :         }
    4123              :     }
    4124              : 
    4125              :   /* The other optimizations can be done only on the non-va_list variants.  */
    4126       105538 :   else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
    4127              :     return false;
    4128              : 
    4129              :   /* If the format specifier was "%s\n", call __builtin_puts(arg).  */
    4130       105288 :   else if (strcmp (fmt_str, target_percent_s_newline) == 0)
    4131              :     {
    4132          180 :       if (!arg || ! POINTER_TYPE_P (TREE_TYPE (arg)))
    4133              :         return false;
    4134          180 :       if (fn_puts)
    4135              :         {
    4136          180 :           gcall *repl = gimple_build_call (fn_puts, 1, arg);
    4137          180 :           replace_call_with_call_and_fold (gsi, repl);
    4138          180 :           return true;
    4139              :         }
    4140              :     }
    4141              : 
    4142              :   /* If the format specifier was "%c", call __builtin_putchar(arg).  */
    4143       105108 :   else if (strcmp (fmt_str, target_percent_c) == 0)
    4144              :     {
    4145           94 :       if (!arg || ! useless_type_conversion_p (integer_type_node,
    4146           47 :                                                TREE_TYPE (arg)))
    4147              :         return false;
    4148           47 :       if (fn_putchar)
    4149              :         {
    4150           47 :           gcall *repl = gimple_build_call (fn_putchar, 1, arg);
    4151           47 :           replace_call_with_call_and_fold (gsi, repl);
    4152           47 :           return true;
    4153              :         }
    4154              :     }
    4155              : 
    4156              :   return false;
    4157              : }
    4158              : 
    4159              : 
    4160              : 
    4161              : /* Fold a call to __builtin_strlen with known length LEN.  */
    4162              : 
    4163              : static bool
    4164       139604 : gimple_fold_builtin_strlen (gimple_stmt_iterator *gsi)
    4165              : {
    4166       139604 :   gimple *stmt = gsi_stmt (*gsi);
    4167       139604 :   tree arg = gimple_call_arg (stmt, 0);
    4168              : 
    4169       139604 :   wide_int minlen;
    4170       139604 :   wide_int maxlen;
    4171              : 
    4172       139604 :   c_strlen_data lendata = { };
    4173       139604 :   if (get_range_strlen (arg, &lendata, /* eltsize = */ 1)
    4174        34973 :       && !lendata.decl
    4175        32010 :       && lendata.minlen && TREE_CODE (lendata.minlen) == INTEGER_CST
    4176       171509 :       && lendata.maxlen && TREE_CODE (lendata.maxlen) == INTEGER_CST)
    4177              :     {
    4178              :       /* The range of lengths refers to either a single constant
    4179              :          string or to the longest and shortest constant string
    4180              :          referenced by the argument of the strlen() call, or to
    4181              :          the strings that can possibly be stored in the arrays
    4182              :          the argument refers to.  */
    4183        31905 :       minlen = wi::to_wide (lendata.minlen);
    4184        31905 :       maxlen = wi::to_wide (lendata.maxlen);
    4185              :     }
    4186              :   else
    4187              :     {
    4188       107699 :       unsigned prec = TYPE_PRECISION (sizetype);
    4189              : 
    4190       107699 :       minlen = wi::shwi (0, prec);
    4191       107699 :       maxlen = wi::to_wide (max_object_size (), prec) - 2;
    4192              :     }
    4193              : 
    4194              :   /* For -fsanitize=address, don't optimize the upper bound of the
    4195              :      length to be able to diagnose UB on non-zero terminated arrays.  */
    4196       139604 :   if (sanitize_flags_p (SANITIZE_ADDRESS))
    4197          278 :     maxlen = wi::max_value (TYPE_PRECISION (sizetype), UNSIGNED);
    4198              : 
    4199       139604 :   if (minlen == maxlen)
    4200              :     {
    4201              :       /* Fold the strlen call to a constant.  */
    4202         1654 :       tree type = TREE_TYPE (lendata.minlen);
    4203         3308 :       tree len = force_gimple_operand_gsi (gsi,
    4204         1654 :                                            wide_int_to_tree (type, minlen),
    4205              :                                            true, NULL, true, GSI_SAME_STMT);
    4206         1654 :       replace_call_with_value (gsi, len);
    4207         1654 :       return true;
    4208              :     }
    4209              : 
    4210              :   /* Set the strlen() range to [0, MAXLEN].  */
    4211       137950 :   if (tree lhs = gimple_call_lhs (stmt))
    4212       137946 :     set_strlen_range (lhs, minlen, maxlen);
    4213              : 
    4214              :   return false;
    4215       139604 : }
    4216              : 
    4217              : static bool
    4218          266 : gimple_fold_builtin_omp_is_initial_device (gimple_stmt_iterator *gsi)
    4219              : {
    4220              : #if ACCEL_COMPILER
    4221              :   replace_call_with_value (gsi, integer_zero_node);
    4222              :   return true;
    4223              : #else
    4224          266 :   if (!ENABLE_OFFLOADING || symtab->state == EXPANSION)
    4225              :     {
    4226            0 :       replace_call_with_value (gsi, integer_one_node);
    4227          266 :       return true;
    4228              :     }
    4229              : #endif
    4230              :   return false;
    4231              : }
    4232              : 
    4233              : /* omp_get_initial_device was in OpenMP 5.0/5.1 explicitly and in
    4234              :    5.0 implicitly the same as omp_get_num_devices; since 6.0 it is
    4235              :    unspecified whether -1 or omp_get_num_devices() is returned.  For
    4236              :    better backward compatibility, use omp_get_num_devices() on the
    4237              :    host - and -1 on the device (where the result is unspecified).  */
    4238              : 
    4239              : static bool
    4240          103 : gimple_fold_builtin_omp_get_initial_device (gimple_stmt_iterator *gsi)
    4241              : {
    4242              : #if ACCEL_COMPILER
    4243              :   replace_call_with_value (gsi, build_int_cst (integer_type_node, -1));
    4244              : #else
    4245          103 :   if (!ENABLE_OFFLOADING)
    4246            0 :     replace_call_with_value (gsi, integer_zero_node);
    4247              :   else
    4248              :     {
    4249              :       tree fn = builtin_decl_explicit (BUILT_IN_OMP_GET_NUM_DEVICES);
    4250              :       gcall *repl = gimple_build_call (fn, 0);
    4251              :       replace_call_with_call_and_fold (gsi, repl);
    4252              :     }
    4253              : #endif
    4254          103 :   return true;
    4255              : }
    4256              : 
    4257              : static bool
    4258          340 : gimple_fold_builtin_omp_get_num_devices (gimple_stmt_iterator *gsi)
    4259              : {
    4260          340 :   if (!ENABLE_OFFLOADING)
    4261              :     {
    4262            0 :       replace_call_with_value (gsi, integer_zero_node);
    4263          340 :       return true;
    4264              :     }
    4265              :   return false;
    4266              : }
    4267              : 
    4268              : /* Fold a call to __builtin_acc_on_device.  */
    4269              : 
    4270              : static bool
    4271         2866 : gimple_fold_builtin_acc_on_device (gimple_stmt_iterator *gsi, tree arg0)
    4272              : {
    4273              :   /* Defer folding until we know which compiler we're in.  */
    4274         2866 :   if (symtab->state != EXPANSION)
    4275              :     return false;
    4276              : 
    4277          554 :   unsigned val_host = GOMP_DEVICE_HOST;
    4278          554 :   unsigned val_dev = GOMP_DEVICE_NONE;
    4279              : 
    4280              : #ifdef ACCEL_COMPILER
    4281              :   val_host = GOMP_DEVICE_NOT_HOST;
    4282              :   val_dev = ACCEL_COMPILER_acc_device;
    4283              : #endif
    4284              : 
    4285          554 :   location_t loc = gimple_location (gsi_stmt (*gsi));
    4286              : 
    4287          554 :   tree host_eq = make_ssa_name (boolean_type_node);
    4288          554 :   gimple *host_ass = gimple_build_assign
    4289          554 :     (host_eq, EQ_EXPR, arg0, build_int_cst (TREE_TYPE (arg0), val_host));
    4290          554 :   gimple_set_location (host_ass, loc);
    4291          554 :   gsi_insert_before (gsi, host_ass, GSI_SAME_STMT);
    4292              : 
    4293          554 :   tree dev_eq = make_ssa_name (boolean_type_node);
    4294          554 :   gimple *dev_ass = gimple_build_assign
    4295          554 :     (dev_eq, EQ_EXPR, arg0, build_int_cst (TREE_TYPE (arg0), val_dev));
    4296          554 :   gimple_set_location (dev_ass, loc);
    4297          554 :   gsi_insert_before (gsi, dev_ass, GSI_SAME_STMT);
    4298              : 
    4299          554 :   tree result = make_ssa_name (boolean_type_node);
    4300          554 :   gimple *result_ass = gimple_build_assign
    4301          554 :     (result, BIT_IOR_EXPR, host_eq, dev_eq);
    4302          554 :   gimple_set_location (result_ass, loc);
    4303          554 :   gsi_insert_before (gsi, result_ass, GSI_SAME_STMT);
    4304              : 
    4305          554 :   replace_call_with_value (gsi, result);
    4306              : 
    4307          554 :   return true;
    4308              : }
    4309              : 
    4310              : /* Fold realloc (0, n) -> malloc (n).  */
    4311              : 
    4312              : static bool
    4313        51060 : gimple_fold_builtin_realloc (gimple_stmt_iterator *gsi)
    4314              : {
    4315        51060 :   gimple *stmt = gsi_stmt (*gsi);
    4316        51060 :   tree arg = gimple_call_arg (stmt, 0);
    4317        51060 :   tree size = gimple_call_arg (stmt, 1);
    4318              : 
    4319       102120 :   if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
    4320              :     return false;
    4321              : 
    4322        51060 :   if (operand_equal_p (arg, null_pointer_node, 0))
    4323              :     {
    4324         1424 :       tree fn_malloc = builtin_decl_implicit (BUILT_IN_MALLOC);
    4325         1424 :       if (fn_malloc)
    4326              :         {
    4327         1424 :           gcall *repl = gimple_build_call (fn_malloc, 1, size);
    4328         1424 :           replace_call_with_call_and_fold (gsi, repl);
    4329         1424 :           return true;
    4330              :         }
    4331              :     }
    4332              :   return false;
    4333              : }
    4334              : 
    4335              : /* Number of bytes into which any type but aggregate, vector or
    4336              :    _BitInt types should fit.  */
    4337              : static constexpr size_t clear_padding_unit
    4338              :   = MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT;
    4339              : /* Buffer size on which __builtin_clear_padding folding code works.  */
    4340              : static const size_t clear_padding_buf_size = 32 * clear_padding_unit;
    4341              : 
    4342              : /* Data passed through __builtin_clear_padding folding.  */
    4343              : struct clear_padding_struct {
    4344              :   location_t loc;
    4345              :   /* 0 during __builtin_clear_padding folding, nonzero during
    4346              :      clear_type_padding_in_mask.  In that case, instead of clearing the
    4347              :      non-padding bits in union_ptr array clear the padding bits in there.  */
    4348              :   bool clear_in_mask;
    4349              :   tree base;
    4350              :   tree alias_type;
    4351              :   gimple_stmt_iterator *gsi;
    4352              :   /* Alignment of buf->base + 0.  */
    4353              :   unsigned align;
    4354              :   /* Offset from buf->base.  Should be always a multiple of UNITS_PER_WORD.  */
    4355              :   HOST_WIDE_INT off;
    4356              :   /* Number of padding bytes before buf->off that don't have padding clear
    4357              :      code emitted yet.  */
    4358              :   HOST_WIDE_INT padding_bytes;
    4359              :   /* The size of the whole object.  Never emit code to touch
    4360              :      buf->base + buf->sz or following bytes.  */
    4361              :   HOST_WIDE_INT sz;
    4362              :   /* Number of bytes recorded in buf->buf.  */
    4363              :   size_t size;
    4364              :   /* When inside union, instead of emitting code we and bits inside of
    4365              :      the union_ptr array.  */
    4366              :   unsigned char *union_ptr;
    4367              :   /* Set bits mean padding bits that need to be cleared by the builtin.  */
    4368              :   unsigned char buf[clear_padding_buf_size + clear_padding_unit];
    4369              : };
    4370              : 
    4371              : /* Emit code to clear padding requested in BUF->buf - set bits
    4372              :    in there stand for padding that should be cleared.  FULL is true
    4373              :    if everything from the buffer should be flushed, otherwise
    4374              :    it can leave up to 2 * clear_padding_unit bytes for further
    4375              :    processing.  */
    4376              : 
    4377              : static void
    4378        49164 : clear_padding_flush (clear_padding_struct *buf, bool full)
    4379              : {
    4380        49164 :   gcc_assert ((clear_padding_unit % UNITS_PER_WORD) == 0);
    4381        49164 :   if (!full && buf->size < 2 * clear_padding_unit)
    4382              :     return;
    4383        50285 :   gcc_assert ((buf->off % UNITS_PER_WORD) == 0);
    4384        49116 :   size_t end = buf->size;
    4385        49116 :   if (!full)
    4386           60 :     end = ((end - clear_padding_unit - 1) / clear_padding_unit
    4387              :            * clear_padding_unit);
    4388        49116 :   size_t padding_bytes = buf->padding_bytes;
    4389        49116 :   if (buf->union_ptr)
    4390              :     {
    4391        48326 :       if (buf->clear_in_mask)
    4392              :         {
    4393              :           /* During clear_type_padding_in_mask, clear the padding
    4394              :              bits set in buf->buf in the buf->union_ptr mask.  */
    4395       398968 :           for (size_t i = 0; i < end; i++)
    4396              :             {
    4397       351045 :               if (buf->buf[i] == (unsigned char) ~0)
    4398         9052 :                 padding_bytes++;
    4399              :               else
    4400              :                 {
    4401       341993 :                   memset (&buf->union_ptr[buf->off + i - padding_bytes],
    4402              :                           0, padding_bytes);
    4403       341993 :                   padding_bytes = 0;
    4404       341993 :                   buf->union_ptr[buf->off + i] &= ~buf->buf[i];
    4405              :                 }
    4406              :             }
    4407        47923 :           if (full)
    4408              :             {
    4409        47923 :               memset (&buf->union_ptr[buf->off + end - padding_bytes],
    4410              :                       0, padding_bytes);
    4411        47923 :               buf->off = 0;
    4412        47923 :               buf->size = 0;
    4413        47923 :               buf->padding_bytes = 0;
    4414              :             }
    4415              :           else
    4416              :             {
    4417            0 :               memmove (buf->buf, buf->buf + end, buf->size - end);
    4418            0 :               buf->off += end;
    4419            0 :               buf->size -= end;
    4420            0 :               buf->padding_bytes = padding_bytes;
    4421              :             }
    4422        47923 :           return;
    4423              :         }
    4424              :       /* Inside of a union, instead of emitting any code, instead
    4425              :          clear all bits in the union_ptr buffer that are clear
    4426              :          in buf.  Whole padding bytes don't clear anything.  */
    4427        19611 :       for (size_t i = 0; i < end; i++)
    4428              :         {
    4429        19208 :           if (buf->buf[i] == (unsigned char) ~0)
    4430         1434 :             padding_bytes++;
    4431              :           else
    4432              :             {
    4433        17774 :               padding_bytes = 0;
    4434        17774 :               buf->union_ptr[buf->off + i] &= buf->buf[i];
    4435              :             }
    4436              :         }
    4437          403 :       if (full)
    4438              :         {
    4439              :           buf->off = 0;
    4440              :           buf->size = 0;
    4441              :           buf->padding_bytes = 0;
    4442              :         }
    4443              :       else
    4444              :         {
    4445            4 :           memmove (buf->buf, buf->buf + end, buf->size - end);
    4446            4 :           buf->off += end;
    4447            4 :           buf->size -= end;
    4448            4 :           buf->padding_bytes = padding_bytes;
    4449              :         }
    4450          403 :       return;
    4451              :     }
    4452          790 :   size_t wordsize = UNITS_PER_WORD;
    4453        28681 :   for (size_t i = 0; i < end; i += wordsize)
    4454              :     {
    4455        27891 :       size_t nonzero_first = wordsize;
    4456        27891 :       size_t nonzero_last = 0;
    4457        27891 :       size_t zero_first = wordsize;
    4458        27891 :       size_t zero_last = 0;
    4459        27891 :       bool all_ones = true, bytes_only = true;
    4460        28184 :       if ((unsigned HOST_WIDE_INT) (buf->off + i + wordsize)
    4461        27891 :           > (unsigned HOST_WIDE_INT) buf->sz)
    4462              :         {
    4463          293 :           gcc_assert (wordsize > 1);
    4464          293 :           wordsize /= 2;
    4465          293 :           i -= wordsize;
    4466          293 :           continue;
    4467              :         }
    4468        27598 :       size_t endsize = end - i > wordsize ? wordsize : end - i;
    4469       247114 :       for (size_t j = i; j < i + endsize; j++)
    4470              :         {
    4471       219516 :           if (buf->buf[j])
    4472              :             {
    4473       168431 :               if (nonzero_first == wordsize)
    4474              :                 {
    4475        21530 :                   nonzero_first = j - i;
    4476        21530 :                   nonzero_last = j - i;
    4477              :                 }
    4478       168431 :               if (nonzero_last != j - i)
    4479          166 :                 all_ones = false;
    4480       168431 :               nonzero_last = j + 1 - i;
    4481              :             }
    4482              :           else
    4483              :             {
    4484        51085 :               if (zero_first == wordsize)
    4485         7081 :                 zero_first = j - i;
    4486        51085 :               zero_last = j + 1 - i;
    4487              :             }
    4488       219516 :           if (buf->buf[j] != 0 && buf->buf[j] != (unsigned char) ~0)
    4489              :             {
    4490          112 :               all_ones = false;
    4491          112 :               bytes_only = false;
    4492              :             }
    4493              :         }
    4494        27598 :       size_t padding_end = i;
    4495        27598 :       if (padding_bytes)
    4496              :         {
    4497        20849 :           if (nonzero_first == 0
    4498        20849 :               && nonzero_last == endsize
    4499        20400 :               && all_ones)
    4500              :             {
    4501              :               /* All bits are padding and we had some padding
    4502              :                  before too.  Just extend it.  */
    4503        20400 :               padding_bytes += endsize;
    4504        20400 :               continue;
    4505              :             }
    4506          449 :           if (all_ones && nonzero_first == 0)
    4507              :             {
    4508            4 :               padding_bytes += nonzero_last;
    4509            4 :               padding_end += nonzero_last;
    4510            4 :               nonzero_first = wordsize;
    4511            4 :               nonzero_last = 0;
    4512              :             }
    4513          445 :           else if (bytes_only && nonzero_first == 0)
    4514              :             {
    4515            0 :               gcc_assert (zero_first && zero_first != wordsize);
    4516            0 :               padding_bytes += zero_first;
    4517            0 :               padding_end += zero_first;
    4518              :             }
    4519          449 :           tree atype, src;
    4520          449 :           if (padding_bytes == 1)
    4521              :             {
    4522           33 :               atype = char_type_node;
    4523           33 :               src = build_zero_cst (char_type_node);
    4524              :             }
    4525              :           else
    4526              :             {
    4527          416 :               atype = build_array_type_nelts (char_type_node, padding_bytes);
    4528          416 :               src = build_constructor (atype, NULL);
    4529              :             }
    4530          449 :           tree dst = build2_loc (buf->loc, MEM_REF, atype, buf->base,
    4531              :                                  build_int_cst (buf->alias_type,
    4532          449 :                                                 buf->off + padding_end
    4533          449 :                                                 - padding_bytes));
    4534          449 :           gimple *g = gimple_build_assign (dst, src);
    4535          449 :           gimple_set_location (g, buf->loc);
    4536          449 :           gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
    4537          449 :           padding_bytes = 0;
    4538          449 :           buf->padding_bytes = 0;
    4539              :         }
    4540         7198 :       if (nonzero_first == wordsize)
    4541              :         /* All bits in a word are 0, there are no padding bits.  */
    4542         6072 :         continue;
    4543         1126 :       if (all_ones && nonzero_last == endsize)
    4544              :         {
    4545              :           /* All bits between nonzero_first and end of word are padding
    4546              :              bits, start counting padding_bytes.  */
    4547          841 :           padding_bytes = nonzero_last - nonzero_first;
    4548          841 :           continue;
    4549              :         }
    4550          285 :       if (bytes_only)
    4551              :         {
    4552              :           /* If bitfields aren't involved in this word, prefer storing
    4553              :              individual bytes or groups of them over performing a RMW
    4554              :              operation on the whole word.  */
    4555          227 :           gcc_assert (i + zero_last <= end);
    4556         1117 :           for (size_t j = padding_end; j < i + zero_last; j++)
    4557              :             {
    4558          890 :               if (buf->buf[j])
    4559              :                 {
    4560              :                   size_t k;
    4561          606 :                   for (k = j; k < i + zero_last; k++)
    4562          606 :                     if (buf->buf[k] == 0)
    4563              :                       break;
    4564          259 :                   HOST_WIDE_INT off = buf->off + j;
    4565          259 :                   tree atype, src;
    4566          259 :                   if (k - j == 1)
    4567              :                     {
    4568          215 :                       atype = char_type_node;
    4569          215 :                       src = build_zero_cst (char_type_node);
    4570              :                     }
    4571              :                   else
    4572              :                     {
    4573           44 :                       atype = build_array_type_nelts (char_type_node, k - j);
    4574           44 :                       src = build_constructor (atype, NULL);
    4575              :                     }
    4576          259 :                   tree dst = build2_loc (buf->loc, MEM_REF, atype,
    4577              :                                          buf->base,
    4578          259 :                                          build_int_cst (buf->alias_type, off));
    4579          259 :                   gimple *g = gimple_build_assign (dst, src);
    4580          259 :                   gimple_set_location (g, buf->loc);
    4581          259 :                   gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
    4582          259 :                   j = k;
    4583              :                 }
    4584              :             }
    4585          227 :           if (nonzero_last == endsize)
    4586           98 :             padding_bytes = nonzero_last - zero_last;
    4587          227 :           continue;
    4588          227 :         }
    4589          177 :       for (size_t eltsz = 1; eltsz <= wordsize; eltsz <<= 1)
    4590              :         {
    4591          177 :           if (nonzero_last - nonzero_first <= eltsz
    4592           58 :               && ((nonzero_first & ~(eltsz - 1))
    4593           58 :                   == ((nonzero_last - 1) & ~(eltsz - 1))))
    4594              :             {
    4595           58 :               tree type;
    4596           58 :               if (eltsz == 1)
    4597            8 :                 type = char_type_node;
    4598              :               else
    4599           50 :                 type = lang_hooks.types.type_for_size (eltsz * BITS_PER_UNIT,
    4600              :                                                        0);
    4601           58 :               size_t start = nonzero_first & ~(eltsz - 1);
    4602           58 :               HOST_WIDE_INT off = buf->off + i + start;
    4603           58 :               tree atype = type;
    4604           58 :               if (eltsz > 1 && buf->align < TYPE_ALIGN (type))
    4605            8 :                 atype = build_aligned_type (type, buf->align);
    4606           58 :               tree dst = build2_loc (buf->loc, MEM_REF, atype, buf->base,
    4607           58 :                                      build_int_cst (buf->alias_type, off));
    4608           58 :               tree src;
    4609           58 :               gimple *g;
    4610           58 :               if (all_ones
    4611           58 :                   && nonzero_first == start
    4612            0 :                   && nonzero_last == start + eltsz)
    4613            0 :                 src = build_zero_cst (type);
    4614              :               else
    4615              :                 {
    4616           58 :                   src = make_ssa_name (type);
    4617           58 :                   tree tmp_dst = unshare_expr (dst);
    4618              :                   /* The folding introduces a read from the tmp_dst, we should
    4619              :                      prevent uninitialized warning analysis from issuing warning
    4620              :                      for such fake read.  In order to suppress warning only for
    4621              :                      this expr, we should set the location of tmp_dst to
    4622              :                      UNKNOWN_LOCATION first, then suppress_warning will call
    4623              :                      set_no_warning_bit to set the no_warning flag only for
    4624              :                      tmp_dst.  */
    4625           58 :                   SET_EXPR_LOCATION (tmp_dst, UNKNOWN_LOCATION);
    4626           58 :                   suppress_warning (tmp_dst, OPT_Wuninitialized);
    4627           58 :                   g = gimple_build_assign (src, tmp_dst);
    4628           58 :                   gimple_set_location (g, buf->loc);
    4629           58 :                   gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
    4630          116 :                   tree mask = native_interpret_expr (type,
    4631           58 :                                                      buf->buf + i + start,
    4632              :                                                      eltsz);
    4633           58 :                   gcc_assert (mask && TREE_CODE (mask) == INTEGER_CST);
    4634           58 :                   mask = fold_build1 (BIT_NOT_EXPR, type, mask);
    4635           58 :                   tree src_masked = make_ssa_name (type);
    4636           58 :                   g = gimple_build_assign (src_masked, BIT_AND_EXPR,
    4637              :                                            src, mask);
    4638           58 :                   gimple_set_location (g, buf->loc);
    4639           58 :                   gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
    4640           58 :                   src = src_masked;
    4641              :                 }
    4642           58 :               g = gimple_build_assign (dst, src);
    4643           58 :               gimple_set_location (g, buf->loc);
    4644           58 :               gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
    4645           58 :               break;
    4646              :             }
    4647              :         }
    4648              :     }
    4649          790 :   if (full)
    4650              :     {
    4651          734 :       if (padding_bytes)
    4652              :         {
    4653          490 :           tree atype, src;
    4654          490 :           if (padding_bytes == 1)
    4655              :             {
    4656          110 :               atype = char_type_node;
    4657          110 :               src = build_zero_cst (char_type_node);
    4658              :             }
    4659              :           else
    4660              :             {
    4661          380 :               atype = build_array_type_nelts (char_type_node, padding_bytes);
    4662          380 :               src = build_constructor (atype, NULL);
    4663              :             }
    4664          490 :           tree dst = build2_loc (buf->loc, MEM_REF, atype, buf->base,
    4665              :                                  build_int_cst (buf->alias_type,
    4666          490 :                                                 buf->off + end
    4667          490 :                                                 - padding_bytes));
    4668          490 :           gimple *g = gimple_build_assign (dst, src);
    4669          490 :           gimple_set_location (g, buf->loc);
    4670          490 :           gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
    4671              :         }
    4672          734 :       size_t end_rem = end % UNITS_PER_WORD;
    4673          734 :       buf->off += end - end_rem;
    4674          734 :       buf->size = end_rem;
    4675          734 :       memset (buf->buf, 0, buf->size);
    4676          734 :       buf->padding_bytes = 0;
    4677              :     }
    4678              :   else
    4679              :     {
    4680           56 :       memmove (buf->buf, buf->buf + end, buf->size - end);
    4681           56 :       buf->off += end;
    4682           56 :       buf->size -= end;
    4683           56 :       buf->padding_bytes = padding_bytes;
    4684              :     }
    4685              : }
    4686              : 
    4687              : /* Append PADDING_BYTES padding bytes.  */
    4688              : 
    4689              : static void
    4690        26583 : clear_padding_add_padding (clear_padding_struct *buf,
    4691              :                            HOST_WIDE_INT padding_bytes)
    4692              : {
    4693        26583 :   if (padding_bytes == 0)
    4694              :     return;
    4695         1763 :   if ((unsigned HOST_WIDE_INT) padding_bytes + buf->size
    4696              :       > (unsigned HOST_WIDE_INT) clear_padding_buf_size)
    4697           42 :     clear_padding_flush (buf, false);
    4698         1763 :   if ((unsigned HOST_WIDE_INT) padding_bytes + buf->size
    4699              :       > (unsigned HOST_WIDE_INT) clear_padding_buf_size)
    4700              :     {
    4701           42 :       memset (buf->buf + buf->size, ~0, clear_padding_buf_size - buf->size);
    4702           42 :       padding_bytes -= clear_padding_buf_size - buf->size;
    4703           42 :       buf->size = clear_padding_buf_size;
    4704           42 :       clear_padding_flush (buf, false);
    4705           42 :       gcc_assert (buf->padding_bytes);
    4706              :       /* At this point buf->buf[0] through buf->buf[buf->size - 1]
    4707              :          is guaranteed to be all ones.  */
    4708           42 :       padding_bytes += buf->size;
    4709           42 :       buf->size = padding_bytes % UNITS_PER_WORD;
    4710           42 :       memset (buf->buf, ~0, buf->size);
    4711           42 :       buf->off += padding_bytes - buf->size;
    4712           42 :       buf->padding_bytes += padding_bytes - buf->size;
    4713              :     }
    4714              :   else
    4715              :     {
    4716         1721 :       memset (buf->buf + buf->size, ~0, padding_bytes);
    4717         1721 :       buf->size += padding_bytes;
    4718              :     }
    4719              : }
    4720              : 
    4721              : static void clear_padding_type (clear_padding_struct *, tree,
    4722              :                                 HOST_WIDE_INT, bool);
    4723              : 
    4724              : /* Clear padding for a bit-field FIELD.  Helper function for
    4725              :    clear_padding_type (to handle bit-fields in RECORD_TYPE) and
    4726              :    clear_padding_union (to handle bit-fields in UNION_TYPE).  */
    4727              : 
    4728              : static HOST_WIDE_INT
    4729          304 : clear_padding_bitfield (clear_padding_struct *buf, tree field,
    4730              :                         HOST_WIDE_INT pos, HOST_WIDE_INT cur_pos,
    4731              :                         bool reversep)
    4732              : {
    4733          304 :   HOST_WIDE_INT fldsz = TYPE_PRECISION (TREE_TYPE (field));
    4734          304 :   HOST_WIDE_INT bpos = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field));
    4735          304 :   bpos %= BITS_PER_UNIT;
    4736          304 :   HOST_WIDE_INT end = ROUND_UP (bpos + fldsz, BITS_PER_UNIT) / BITS_PER_UNIT;
    4737          304 :   if (pos + end > cur_pos)
    4738              :     {
    4739          226 :       if ((unsigned HOST_WIDE_INT) end
    4740              :           > (unsigned HOST_WIDE_INT) clear_padding_buf_size / 2)
    4741              :         {
    4742            6 :           if ((unsigned HOST_WIDE_INT) pos + clear_padding_unit
    4743            6 :               > (unsigned HOST_WIDE_INT) cur_pos)
    4744              :             {
    4745            6 :               clear_padding_add_padding (buf,
    4746            6 :                                          pos + clear_padding_unit - cur_pos);
    4747            6 :               cur_pos = pos + clear_padding_unit;
    4748              :             }
    4749              :         }
    4750              :       else
    4751              :         {
    4752          220 :           clear_padding_add_padding (buf, pos + end - cur_pos);
    4753          220 :           cur_pos = pos + end;
    4754              :         }
    4755              :     }
    4756          304 :   gcc_assert (cur_pos > pos
    4757              :               && ((unsigned HOST_WIDE_INT) buf->size
    4758              :                   >= (unsigned HOST_WIDE_INT) cur_pos - pos));
    4759          304 :   unsigned char *p = buf->buf + buf->size - (cur_pos - pos);
    4760          304 :   HOST_WIDE_INT fldszu = 0;
    4761          310 :   auto bitfld_adjust = [&] {
    4762            6 :     memset (p, 0, buf->buf + buf->size - p);
    4763            6 :     fldszu -= buf->buf + buf->size - p;
    4764            6 :     clear_padding_flush (buf, false);
    4765            6 :     while ((unsigned HOST_WIDE_INT) fldszu
    4766           20 :            > (unsigned HOST_WIDE_INT) 2 * clear_padding_unit)
    4767              :       {
    4768           14 :         HOST_WIDE_INT this_sz
    4769           14 :           = MIN ((HOST_WIDE_INT) (clear_padding_buf_size - buf->size),
    4770              :                  (HOST_WIDE_INT) (fldszu - 2 * clear_padding_unit));
    4771           14 :         memset (buf->buf + buf->size, 0, this_sz);
    4772           14 :         fldszu -= this_sz;
    4773           14 :         cur_pos += this_sz;
    4774           14 :         buf->size += this_sz;
    4775           14 :         clear_padding_flush (buf, false);
    4776              :       }
    4777            6 :     clear_padding_add_padding (buf, pos + end - cur_pos);
    4778            6 :     cur_pos = pos + end;
    4779            6 :     pos = cur_pos - fldszu - ((fldsz % BITS_PER_UNIT) != 0);
    4780            6 :     gcc_assert (cur_pos > pos && ((unsigned HOST_WIDE_INT) buf->size
    4781              :                                   >= (unsigned HOST_WIDE_INT) cur_pos - pos));
    4782            6 :     p = buf->buf + buf->size - (cur_pos - pos);
    4783          310 :   };
    4784          304 :   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
    4785              :     sorry_at (buf->loc, "PDP11 bit-field handling unsupported"
    4786              :                         " in %qs", "__builtin_clear_padding");
    4787          304 :   else if (reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
    4788              :     {
    4789              :       /* Big endian.  */
    4790            2 :       if (bpos + fldsz <= BITS_PER_UNIT)
    4791            2 :         *p &= ~(((1 << fldsz) - 1) << (BITS_PER_UNIT - bpos - fldsz));
    4792              :       else
    4793              :         {
    4794            0 :           if (bpos)
    4795              :             {
    4796            0 :               *p &= ~(((1U << BITS_PER_UNIT) - 1) >> bpos);
    4797            0 :               p++;
    4798            0 :               fldsz -= BITS_PER_UNIT - bpos;
    4799              :             }
    4800            0 :           fldszu = fldsz / BITS_PER_UNIT;
    4801            0 :           if (cur_pos < pos + end)
    4802            0 :             bitfld_adjust ();
    4803            0 :           memset (p, 0, fldszu);
    4804            0 :           p += fldszu;
    4805            0 :           fldsz %= BITS_PER_UNIT;
    4806            0 :           if (fldsz)
    4807            0 :             *p &= ((1U << BITS_PER_UNIT) - 1) >> fldsz;
    4808              :         }
    4809              :     }
    4810              :   else
    4811              :     {
    4812              :       /* Little endian.  */
    4813          302 :       if (bpos + fldsz <= BITS_PER_UNIT)
    4814          175 :         *p &= ~(((1 << fldsz) - 1) << bpos);
    4815              :       else
    4816              :         {
    4817          127 :           if (bpos)
    4818              :             {
    4819           33 :               *p &= ~(((1 << BITS_PER_UNIT) - 1) << bpos);
    4820           33 :               p++;
    4821           33 :               fldsz -= BITS_PER_UNIT - bpos;
    4822              :             }
    4823          127 :           fldszu = fldsz / BITS_PER_UNIT;
    4824          127 :           if (cur_pos < pos + end)
    4825            6 :             bitfld_adjust ();
    4826          127 :           memset (p, 0, fldszu);
    4827          127 :           p += fldszu;
    4828          127 :           fldsz %= BITS_PER_UNIT;
    4829          127 :           if (fldsz)
    4830           82 :             *p &= ~((1 << fldsz) - 1);
    4831              :         }
    4832              :     }
    4833          304 :   return cur_pos;
    4834              : }
    4835              : 
    4836              : /* Clear padding bits of union type TYPE.  */
    4837              : 
    4838              : static void
    4839          131 : clear_padding_union (clear_padding_struct *buf, tree type,
    4840              :                      HOST_WIDE_INT sz, bool for_auto_init)
    4841              : {
    4842          131 :   clear_padding_struct *union_buf;
    4843          131 :   HOST_WIDE_INT start_off = 0, next_off = 0;
    4844          131 :   size_t start_size = 0;
    4845          131 :   if (buf->union_ptr)
    4846              :     {
    4847           42 :       start_off = buf->off + buf->size;
    4848           42 :       next_off = start_off + sz;
    4849           42 :       start_size = start_off % UNITS_PER_WORD;
    4850           42 :       start_off -= start_size;
    4851           42 :       clear_padding_flush (buf, true);
    4852           42 :       union_buf = buf;
    4853              :     }
    4854              :   else
    4855              :     {
    4856           89 :       if (sz + buf->size > clear_padding_buf_size)
    4857            1 :         clear_padding_flush (buf, false);
    4858           89 :       union_buf = XALLOCA (clear_padding_struct);
    4859           89 :       union_buf->loc = buf->loc;
    4860           89 :       union_buf->clear_in_mask = buf->clear_in_mask;
    4861           89 :       union_buf->base = NULL_TREE;
    4862           89 :       union_buf->alias_type = NULL_TREE;
    4863           89 :       union_buf->gsi = NULL;
    4864           89 :       union_buf->align = 0;
    4865           89 :       union_buf->off = 0;
    4866           89 :       union_buf->padding_bytes = 0;
    4867           89 :       union_buf->sz = sz;
    4868           89 :       union_buf->size = 0;
    4869           89 :       if (sz + buf->size <= clear_padding_buf_size)
    4870           88 :         union_buf->union_ptr = buf->buf + buf->size;
    4871              :       else
    4872            1 :         union_buf->union_ptr = XNEWVEC (unsigned char, sz);
    4873           89 :       memset (union_buf->union_ptr, ~0, sz);
    4874              :     }
    4875              : 
    4876         1202 :   for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
    4877         1071 :     if (TREE_CODE (field) == FIELD_DECL && !DECL_PADDING_P (field))
    4878              :       {
    4879          365 :         if (DECL_SIZE_UNIT (field) == NULL_TREE)
    4880              :           {
    4881            8 :             if (TREE_TYPE (field) == error_mark_node)
    4882            0 :               continue;
    4883            8 :             gcc_assert (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
    4884              :                         && !COMPLETE_TYPE_P (TREE_TYPE (field)));
    4885            8 :             if (!buf->clear_in_mask && !for_auto_init)
    4886            8 :               error_at (buf->loc, "flexible array member %qD does not have "
    4887              :                                   "well defined padding bits for %qs",
    4888              :                         field, "__builtin_clear_padding");
    4889            8 :             continue;
    4890              :           }
    4891          357 :         HOST_WIDE_INT fldsz = tree_to_shwi (DECL_SIZE_UNIT (field));
    4892          357 :         gcc_assert (union_buf->size == 0);
    4893          357 :         union_buf->off = start_off;
    4894          357 :         union_buf->size = start_size;
    4895          357 :         memset (union_buf->buf, ~0, start_size);
    4896          357 :         if (DECL_BIT_FIELD (field))
    4897              :           {
    4898            6 :             HOST_WIDE_INT cur_pos
    4899           12 :               = clear_padding_bitfield (union_buf, field, 0, 0,
    4900            6 :                                         TYPE_REVERSE_STORAGE_ORDER (type));
    4901            6 :             gcc_assert (cur_pos == fldsz);
    4902              :           }
    4903              :         else
    4904          351 :           clear_padding_type (union_buf, TREE_TYPE (field), fldsz,
    4905              :                               for_auto_init);
    4906          357 :         clear_padding_add_padding (union_buf, sz - fldsz);
    4907          357 :         clear_padding_flush (union_buf, true);
    4908              :       }
    4909              : 
    4910          131 :   if (buf == union_buf)
    4911              :     {
    4912           42 :       buf->off = next_off;
    4913           42 :       buf->size = next_off % UNITS_PER_WORD;
    4914           42 :       buf->off -= buf->size;
    4915           42 :       memset (buf->buf, ~0, buf->size);
    4916              :     }
    4917           89 :   else if (sz + buf->size <= clear_padding_buf_size)
    4918           88 :     buf->size += sz;
    4919              :   else
    4920              :     {
    4921            1 :       unsigned char *union_ptr = union_buf->union_ptr;
    4922            4 :       while (sz)
    4923              :         {
    4924            3 :           clear_padding_flush (buf, false);
    4925            3 :           HOST_WIDE_INT this_sz
    4926            3 :             = MIN ((unsigned HOST_WIDE_INT) sz,
    4927              :                    clear_padding_buf_size - buf->size);
    4928            3 :           memcpy (buf->buf + buf->size, union_ptr, this_sz);
    4929            3 :           buf->size += this_sz;
    4930            3 :           union_ptr += this_sz;
    4931            3 :           sz -= this_sz;
    4932              :         }
    4933            1 :       XDELETE (union_buf->union_ptr);
    4934              :     }
    4935          131 : }
    4936              : 
    4937              : /* The only known floating point formats with padding bits are the
    4938              :    IEEE extended ones.  */
    4939              : 
    4940              : static bool
    4941        48400 : clear_padding_real_needs_padding_p (tree type)
    4942              : {
    4943        48400 :   const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
    4944        48400 :   return (fmt->b == 2
    4945        47789 :           && fmt->signbit_ro == fmt->signbit_rw
    4946        96189 :           && (fmt->signbit_ro == 79 || fmt->signbit_ro == 95));
    4947              : }
    4948              : 
    4949              : /* _BitInt has padding bits if it isn't extended in the ABI and has smaller
    4950              :    precision than bits in limb or corresponding number of limbs.  */
    4951              : 
    4952              : static bool
    4953           60 : clear_padding_bitint_needs_padding_p (tree type)
    4954              : {
    4955           60 :   struct bitint_info info;
    4956           60 :   bool ok = targetm.c.bitint_type_info (TYPE_PRECISION (type), &info);
    4957           60 :   gcc_assert (ok);
    4958           60 :   if (info.extended)
    4959              :     return false;
    4960           60 :   scalar_int_mode limb_mode = as_a <scalar_int_mode> (info.abi_limb_mode);
    4961           60 :   if (TYPE_PRECISION (type) < GET_MODE_PRECISION (limb_mode))
    4962              :     return true;
    4963            4 :   else if (TYPE_PRECISION (type) == GET_MODE_PRECISION (limb_mode))
    4964              :     return false;
    4965              :   else
    4966            4 :     return (((unsigned) TYPE_PRECISION (type))
    4967            4 :             % GET_MODE_PRECISION (limb_mode)) != 0;
    4968              : }
    4969              : 
    4970              : /* Return true if TYPE might contain any padding bits.  */
    4971              : 
    4972              : bool
    4973       953556 : clear_padding_type_may_have_padding_p (tree type)
    4974              : {
    4975      1093933 :   switch (TREE_CODE (type))
    4976              :     {
    4977              :     case RECORD_TYPE:
    4978              :     case UNION_TYPE:
    4979              :       return true;
    4980       140377 :     case ARRAY_TYPE:
    4981       140377 :     case COMPLEX_TYPE:
    4982       140377 :     case VECTOR_TYPE:
    4983       140377 :       return clear_padding_type_may_have_padding_p (TREE_TYPE (type));
    4984         1935 :     case REAL_TYPE:
    4985         1935 :       return clear_padding_real_needs_padding_p (type);
    4986           63 :     case ENUMERAL_TYPE:
    4987           63 :       if (BITINT_TYPE_P (type))
    4988            0 :         return clear_padding_bitint_needs_padding_p (type);
    4989              :       return false;
    4990           60 :     case BITINT_TYPE:
    4991           60 :       return clear_padding_bitint_needs_padding_p (type);
    4992              :     default:
    4993              :       return false;
    4994              :     }
    4995              : }
    4996              : 
    4997              : /* Return true if TYPE has padding bits aside from those in fields,
    4998              :    elements, etc.  */
    4999              : 
    5000              : bool
    5001      1205228 : type_has_padding_at_level_p (tree type)
    5002              : {
    5003      1205228 :   switch (TREE_CODE (type))
    5004              :     {
    5005      1061946 :     case RECORD_TYPE:
    5006      1061946 :       {
    5007      1061946 :         tree bitpos = size_zero_node;
    5008              :         /* Expect fields to be sorted by bit position.  */
    5009      8731842 :         for (tree f = TYPE_FIELDS (type); f; f = DECL_CHAIN (f))
    5010      7675758 :           if (TREE_CODE (f) == FIELD_DECL)
    5011              :             {
    5012      2310282 :               if (DECL_PADDING_P (f))
    5013              :                 return true;
    5014      2310279 :               tree pos = bit_position (f);
    5015      2310279 :               if (simple_cst_equal (bitpos, pos) != 1)
    5016              :                 return true;
    5017      2304444 :               if (!DECL_SIZE (f))
    5018              :                 return true;
    5019      2304420 :               bitpos = int_const_binop (PLUS_EXPR, pos, DECL_SIZE (f));
    5020              :             }
    5021      1056084 :         if (simple_cst_equal (bitpos, TYPE_SIZE (type)) != 1)
    5022        12640 :           return true;
    5023              :         return false;
    5024              :       }
    5025            3 :     case UNION_TYPE:
    5026            3 :     case QUAL_UNION_TYPE:
    5027            3 :       bool any_fields;
    5028            3 :       any_fields = false;
    5029              :       /* If any of the fields is smaller than the whole, there is padding.  */
    5030            6 :       for (tree f = TYPE_FIELDS (type); f; f = DECL_CHAIN (f))
    5031            3 :         if (TREE_CODE (f) != FIELD_DECL || TREE_TYPE (f) == error_mark_node)
    5032            3 :           continue;
    5033            0 :         else if (simple_cst_equal (DECL_SIZE (f), TYPE_SIZE (type)) != 1)
    5034              :           return true;
    5035              :         else
    5036              :           any_fields = true;
    5037              :       /* If the union doesn't have any fields and still has non-zero size,
    5038              :          all of it is padding.  */
    5039            3 :       if (!any_fields && !integer_zerop (TYPE_SIZE (type)))
    5040            3 :         return true;
    5041              :       return false;
    5042              :     case ARRAY_TYPE:
    5043              :     case COMPLEX_TYPE:
    5044              :     case VECTOR_TYPE:
    5045              :       /* No recursing here, no padding at this level.  */
    5046              :       return false;
    5047            0 :     case REAL_TYPE:
    5048            0 :       return clear_padding_real_needs_padding_p (type);
    5049            0 :     case ENUMERAL_TYPE:
    5050            0 :       if (BITINT_TYPE_P (type))
    5051            0 :         return clear_padding_bitint_needs_padding_p (type);
    5052              :       return false;
    5053            0 :     case BITINT_TYPE:
    5054            0 :       return clear_padding_bitint_needs_padding_p (type);
    5055              :     default:
    5056              :       return false;
    5057              :     }
    5058              : }
    5059              : 
    5060              : /* Emit a runtime loop:
    5061              :    for (; buf.base != end; buf.base += sz)
    5062              :      __builtin_clear_padding (buf.base);  */
    5063              : 
    5064              : static void
    5065          114 : clear_padding_emit_loop (clear_padding_struct *buf, tree type,
    5066              :                          tree end, bool for_auto_init)
    5067              : {
    5068          114 :   tree l1 = create_artificial_label (buf->loc);
    5069          114 :   tree l2 = create_artificial_label (buf->loc);
    5070          114 :   tree l3 = create_artificial_label (buf->loc);
    5071          114 :   gimple *g = gimple_build_goto (l2);
    5072          114 :   gimple_set_location (g, buf->loc);
    5073          114 :   gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
    5074          114 :   g = gimple_build_label (l1);
    5075          114 :   gimple_set_location (g, buf->loc);
    5076          114 :   gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
    5077          114 :   clear_padding_type (buf, type, buf->sz, for_auto_init);
    5078          114 :   clear_padding_flush (buf, true);
    5079          114 :   g = gimple_build_assign (buf->base, POINTER_PLUS_EXPR, buf->base,
    5080          114 :                            size_int (buf->sz));
    5081          114 :   gimple_set_location (g, buf->loc);
    5082          114 :   gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
    5083          114 :   g = gimple_build_label (l2);
    5084          114 :   gimple_set_location (g, buf->loc);
    5085          114 :   gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
    5086          114 :   g = gimple_build_cond (NE_EXPR, buf->base, end, l1, l3);
    5087          114 :   gimple_set_location (g, buf->loc);
    5088          114 :   gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
    5089          114 :   g = gimple_build_label (l3);
    5090          114 :   gimple_set_location (g, buf->loc);
    5091          114 :   gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
    5092          114 : }
    5093              : 
    5094              : /* Clear padding bits for TYPE.  Called recursively from
    5095              :    gimple_fold_builtin_clear_padding.  If FOR_AUTO_INIT is true,
    5096              :    the __builtin_clear_padding is not called by the end user,
    5097              :    instead, it's inserted by the compiler to initialize the
    5098              :    paddings of automatic variable.  Therefore, we should not
    5099              :    emit the error messages for flexible array members to confuse
    5100              :    the end user.  */
    5101              : 
    5102              : static void
    5103        84012 : clear_padding_type (clear_padding_struct *buf, tree type,
    5104              :                     HOST_WIDE_INT sz, bool for_auto_init)
    5105              : {
    5106        84012 :   switch (TREE_CODE (type))
    5107              :     {
    5108        10307 :     case RECORD_TYPE:
    5109        10307 :       HOST_WIDE_INT cur_pos;
    5110        10307 :       cur_pos = 0;
    5111       793676 :       for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
    5112       783369 :         if (TREE_CODE (field) == FIELD_DECL && !DECL_PADDING_P (field))
    5113              :           {
    5114        24983 :             tree ftype = TREE_TYPE (field);
    5115        24983 :             if (DECL_BIT_FIELD (field))
    5116              :               {
    5117          298 :                 HOST_WIDE_INT fldsz = TYPE_PRECISION (ftype);
    5118          298 :                 if (fldsz == 0)
    5119            0 :                   continue;
    5120          298 :                 HOST_WIDE_INT pos = int_byte_position (field);
    5121          298 :                 if (pos >= sz)
    5122            0 :                   continue;
    5123          298 :                 if (pos > cur_pos)
    5124              :                   {
    5125           28 :                     clear_padding_add_padding (buf, pos - cur_pos);
    5126           28 :                     cur_pos = pos;
    5127              :                   }
    5128          298 :                 cur_pos
    5129          298 :                   = clear_padding_bitfield (buf, field, pos, cur_pos,
    5130          298 :                                             TYPE_REVERSE_STORAGE_ORDER (type));
    5131              :               }
    5132        24685 :             else if (DECL_SIZE_UNIT (field) == NULL_TREE)
    5133              :               {
    5134           32 :                 if (ftype == error_mark_node)
    5135            0 :                   continue;
    5136           32 :                 gcc_assert (TREE_CODE (ftype) == ARRAY_TYPE
    5137              :                             && !COMPLETE_TYPE_P (ftype));
    5138           32 :                 if (!buf->clear_in_mask && !for_auto_init)
    5139           24 :                   error_at (buf->loc, "flexible array member %qD does not "
    5140              :                                       "have well defined padding bits for %qs",
    5141              :                             field, "__builtin_clear_padding");
    5142              :               }
    5143        24653 :             else if (is_empty_type (ftype))
    5144         8994 :               continue;
    5145              :             else
    5146              :               {
    5147        15659 :                 HOST_WIDE_INT pos = int_byte_position (field);
    5148        15659 :                 if (pos >= sz)
    5149            0 :                   continue;
    5150        15659 :                 HOST_WIDE_INT fldsz = tree_to_shwi (DECL_SIZE_UNIT (field));
    5151        15659 :                 gcc_assert (pos >= 0 && fldsz >= 0 && pos >= cur_pos);
    5152        15659 :                 clear_padding_add_padding (buf, pos - cur_pos);
    5153        15659 :                 cur_pos = pos;
    5154        15659 :                 if (tree asbase = lang_hooks.types.classtype_as_base (field))
    5155          300 :                   ftype = asbase;
    5156        15659 :                 clear_padding_type (buf, ftype, fldsz, for_auto_init);
    5157        15659 :                 cur_pos += fldsz;
    5158              :               }
    5159              :           }
    5160        10307 :       gcc_assert (sz >= cur_pos);
    5161        10307 :       clear_padding_add_padding (buf, sz - cur_pos);
    5162        10307 :       break;
    5163          326 :     case ARRAY_TYPE:
    5164          326 :       HOST_WIDE_INT nelts, fldsz;
    5165          326 :       fldsz = int_size_in_bytes (TREE_TYPE (type));
    5166          326 :       if (fldsz == 0)
    5167              :         break;
    5168          312 :       nelts = sz / fldsz;
    5169          312 :       if (nelts > 1
    5170          305 :           && sz > 8 * UNITS_PER_WORD
    5171           78 :           && buf->union_ptr == NULL
    5172          390 :           && clear_padding_type_may_have_padding_p (TREE_TYPE (type)))
    5173              :         {
    5174              :           /* For sufficiently large array of more than one elements,
    5175              :              emit a runtime loop to keep code size manageable.  */
    5176           66 :           tree base = buf->base;
    5177           66 :           unsigned int prev_align = buf->align;
    5178           66 :           HOST_WIDE_INT off = buf->off + buf->size;
    5179           66 :           HOST_WIDE_INT prev_sz = buf->sz;
    5180           66 :           clear_padding_flush (buf, true);
    5181           66 :           tree elttype = TREE_TYPE (type);
    5182           66 :           buf->base = create_tmp_var (build_pointer_type (elttype));
    5183           66 :           tree end = make_ssa_name (TREE_TYPE (buf->base));
    5184           66 :           gimple *g = gimple_build_assign (buf->base, POINTER_PLUS_EXPR,
    5185           66 :                                            base, size_int (off));
    5186           66 :           gimple_set_location (g, buf->loc);
    5187           66 :           gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
    5188           66 :           g = gimple_build_assign (end, POINTER_PLUS_EXPR, buf->base,
    5189           66 :                                    size_int (sz));
    5190           66 :           gimple_set_location (g, buf->loc);
    5191           66 :           gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
    5192           66 :           buf->sz = fldsz;
    5193           66 :           buf->align = TYPE_ALIGN (elttype);
    5194           66 :           buf->off = 0;
    5195           66 :           buf->size = 0;
    5196           66 :           clear_padding_emit_loop (buf, elttype, end, for_auto_init);
    5197           66 :           off += sz;
    5198           66 :           buf->base = base;
    5199           66 :           buf->sz = prev_sz;
    5200           66 :           buf->align = prev_align;
    5201           66 :           buf->size = off % UNITS_PER_WORD;
    5202           66 :           buf->off = off - buf->size;
    5203           66 :           memset (buf->buf, 0, buf->size);
    5204           66 :           break;
    5205              :         }
    5206         1180 :       for (HOST_WIDE_INT i = 0; i < nelts; i++)
    5207          934 :         clear_padding_type (buf, TREE_TYPE (type), fldsz, for_auto_init);
    5208              :       break;
    5209          131 :     case UNION_TYPE:
    5210          131 :       clear_padding_union (buf, type, sz, for_auto_init);
    5211          131 :       break;
    5212        46465 :     case REAL_TYPE:
    5213        46465 :       gcc_assert ((size_t) sz <= clear_padding_unit);
    5214        46465 :       if ((unsigned HOST_WIDE_INT) sz + buf->size > clear_padding_buf_size)
    5215            0 :         clear_padding_flush (buf, false);
    5216        46465 :       if (clear_padding_real_needs_padding_p (type))
    5217              :         {
    5218              :           /* Use native_interpret_real + native_encode_expr to figure out
    5219              :              which bits are padding.  */
    5220         1587 :           memset (buf->buf + buf->size, ~0, sz);
    5221         1587 :           tree cst = native_interpret_real (type, buf->buf + buf->size, sz);
    5222         1587 :           gcc_assert (cst && TREE_CODE (cst) == REAL_CST);
    5223         1587 :           int len = native_encode_expr (cst, buf->buf + buf->size, sz);
    5224         1587 :           gcc_assert (len > 0 && (size_t) len == (size_t) sz);
    5225        26979 :           for (size_t i = 0; i < (size_t) sz; i++)
    5226        25392 :             buf->buf[buf->size + i] ^= ~0;
    5227              :         }
    5228              :       else
    5229        44878 :         memset (buf->buf + buf->size, 0, sz);
    5230        46465 :       buf->size += sz;
    5231        46465 :       break;
    5232            0 :     case COMPLEX_TYPE:
    5233            0 :       fldsz = int_size_in_bytes (TREE_TYPE (type));
    5234            0 :       clear_padding_type (buf, TREE_TYPE (type), fldsz, for_auto_init);
    5235            0 :       clear_padding_type (buf, TREE_TYPE (type), fldsz, for_auto_init);
    5236            0 :       break;
    5237         5180 :     case VECTOR_TYPE:
    5238         5180 :       nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
    5239         5180 :       fldsz = int_size_in_bytes (TREE_TYPE (type));
    5240        28828 :       for (HOST_WIDE_INT i = 0; i < nelts; i++)
    5241        18468 :         clear_padding_type (buf, TREE_TYPE (type), fldsz, for_auto_init);
    5242              :       break;
    5243            7 :     case NULLPTR_TYPE:
    5244            7 :       gcc_assert ((size_t) sz <= clear_padding_unit);
    5245            7 :       if ((unsigned HOST_WIDE_INT) sz + buf->size > clear_padding_buf_size)
    5246            0 :         clear_padding_flush (buf, false);
    5247            7 :       memset (buf->buf + buf->size, ~0, sz);
    5248            7 :       buf->size += sz;
    5249            7 :       break;
    5250            7 :     case BITINT_TYPE:
    5251            7 :     do_bitint:
    5252            7 :       {
    5253            7 :         struct bitint_info info;
    5254            7 :         bool ok = targetm.c.bitint_type_info (TYPE_PRECISION (type), &info);
    5255            7 :         gcc_assert (ok);
    5256            7 :         scalar_int_mode limb_mode
    5257            7 :           = as_a <scalar_int_mode> (info.abi_limb_mode);
    5258            7 :         if (TYPE_PRECISION (type) <= GET_MODE_PRECISION (limb_mode))
    5259              :           {
    5260            5 :             gcc_assert ((size_t) sz <= clear_padding_unit);
    5261            5 :             if ((unsigned HOST_WIDE_INT) sz + buf->size
    5262              :                 > clear_padding_buf_size)
    5263            0 :               clear_padding_flush (buf, false);
    5264            5 :             if (!info.extended
    5265            5 :                 && TYPE_PRECISION (type) < GET_MODE_PRECISION (limb_mode))
    5266              :               {
    5267            5 :                 int tprec = GET_MODE_PRECISION (limb_mode);
    5268            5 :                 int prec = TYPE_PRECISION (type);
    5269            5 :                 tree t = build_nonstandard_integer_type (tprec, 1);
    5270            5 :                 tree cst = wide_int_to_tree (t, wi::mask (prec, true, tprec));
    5271            5 :                 int len = native_encode_expr (cst, buf->buf + buf->size, sz);
    5272            5 :                 gcc_assert (len > 0 && (size_t) len == (size_t) sz);
    5273              :               }
    5274              :             else
    5275            0 :               memset (buf->buf + buf->size, 0, sz);
    5276            5 :             buf->size += sz;
    5277            5 :             break;
    5278              :           }
    5279            2 :         tree limbtype
    5280            2 :           = build_nonstandard_integer_type (GET_MODE_PRECISION (limb_mode), 1);
    5281            2 :         fldsz = int_size_in_bytes (limbtype);
    5282            2 :         nelts = int_size_in_bytes (type) / fldsz;
    5283           13 :         for (HOST_WIDE_INT i = 0; i < nelts; i++)
    5284              :           {
    5285           11 :             if (!info.extended
    5286           11 :                 && i == (info.big_endian ? 0 : nelts - 1)
    5287           13 :                 && (((unsigned) TYPE_PRECISION (type))
    5288            2 :                     % TYPE_PRECISION (limbtype)) != 0)
    5289              :               {
    5290            2 :                 int tprec = GET_MODE_PRECISION (limb_mode);
    5291            2 :                 int prec = (((unsigned) TYPE_PRECISION (type)) % tprec);
    5292            2 :                 tree cst = wide_int_to_tree (limbtype,
    5293            2 :                                              wi::mask (prec, true, tprec));
    5294            2 :                 int len = native_encode_expr (cst, buf->buf + buf->size,
    5295              :                                               fldsz);
    5296            2 :                 gcc_assert (len > 0 && (size_t) len == (size_t) fldsz);
    5297            2 :                 buf->size += fldsz;
    5298              :               }
    5299              :             else
    5300            9 :               clear_padding_type (buf, limbtype, fldsz, for_auto_init);
    5301              :           }
    5302              :         break;
    5303              :       }
    5304            0 :     case ENUMERAL_TYPE:
    5305            0 :       if (BITINT_TYPE_P (type))
    5306            0 :         goto do_bitint;
    5307              :       /* FALLTHRU */
    5308        21589 :     default:
    5309        21589 :       gcc_assert ((size_t) sz <= clear_padding_unit);
    5310        21589 :       if ((unsigned HOST_WIDE_INT) sz + buf->size > clear_padding_buf_size)
    5311            0 :         clear_padding_flush (buf, false);
    5312        21589 :       memset (buf->buf + buf->size, 0, sz);
    5313        21589 :       buf->size += sz;
    5314        21589 :       break;
    5315              :     }
    5316        84012 : }
    5317              : 
    5318              : /* Clear padding bits of TYPE in MASK.  */
    5319              : 
    5320              : void
    5321        47923 : clear_type_padding_in_mask (tree type, unsigned char *mask)
    5322              : {
    5323        47923 :   clear_padding_struct buf;
    5324        47923 :   buf.loc = UNKNOWN_LOCATION;
    5325        47923 :   buf.clear_in_mask = true;
    5326        47923 :   buf.base = NULL_TREE;
    5327        47923 :   buf.alias_type = NULL_TREE;
    5328        47923 :   buf.gsi = NULL;
    5329        47923 :   buf.align = 0;
    5330        47923 :   buf.off = 0;
    5331        47923 :   buf.padding_bytes = 0;
    5332        47923 :   buf.sz = int_size_in_bytes (type);
    5333        47923 :   buf.size = 0;
    5334        47923 :   buf.union_ptr = mask;
    5335        47923 :   clear_padding_type (&buf, type, buf.sz, false);
    5336        47923 :   clear_padding_flush (&buf, true);
    5337        47923 : }
    5338              : 
    5339              : /* Fold __builtin_clear_padding builtin.  */
    5340              : 
    5341              : static bool
    5342          640 : gimple_fold_builtin_clear_padding (gimple_stmt_iterator *gsi)
    5343              : {
    5344          640 :   gimple *stmt = gsi_stmt (*gsi);
    5345          640 :   gcc_assert (gimple_call_num_args (stmt) == 2);
    5346          640 :   tree ptr = gimple_call_arg (stmt, 0);
    5347          640 :   tree typearg = gimple_call_arg (stmt, 1);
    5348              :   /* The 2nd argument of __builtin_clear_padding's value is used to
    5349              :      distinguish whether this call is made by the user or by the compiler
    5350              :      for automatic variable initialization.  */
    5351          640 :   bool for_auto_init = (bool) TREE_INT_CST_LOW (typearg);
    5352          640 :   tree type = TREE_TYPE (TREE_TYPE (typearg));
    5353          640 :   location_t loc = gimple_location (stmt);
    5354          640 :   clear_padding_struct buf;
    5355          640 :   gimple_stmt_iterator gsiprev = *gsi;
    5356              :   /* This should be folded during the lower pass.  */
    5357         1280 :   gcc_assert (!gimple_in_ssa_p (cfun) && cfun->cfg == NULL);
    5358          640 :   gcc_assert (COMPLETE_TYPE_P (type));
    5359          640 :   gsi_prev (&gsiprev);
    5360              : 
    5361          640 :   buf.loc = loc;
    5362          640 :   buf.clear_in_mask = false;
    5363          640 :   buf.base = ptr;
    5364          640 :   buf.alias_type = NULL_TREE;
    5365          640 :   buf.gsi = gsi;
    5366          640 :   buf.align = get_pointer_alignment (ptr);
    5367          640 :   unsigned int talign = min_align_of_type (type) * BITS_PER_UNIT;
    5368          640 :   buf.align = MAX (buf.align, talign);
    5369          640 :   buf.off = 0;
    5370          640 :   buf.padding_bytes = 0;
    5371          640 :   buf.size = 0;
    5372          640 :   buf.sz = int_size_in_bytes (type);
    5373          640 :   buf.union_ptr = NULL;
    5374          640 :   if (buf.sz < 0 && int_size_in_bytes (strip_array_types (type)) < 0)
    5375            1 :     sorry_at (loc, "%s not supported for variable length aggregates",
    5376              :               "__builtin_clear_padding");
    5377              :   /* The implementation currently assumes 8-bit host and target
    5378              :      chars which is the case for all currently supported targets
    5379              :      and hosts and is required e.g. for native_{encode,interpret}* APIs.  */
    5380          639 :   else if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
    5381              :     sorry_at (loc, "%s not supported on this target",
    5382              :               "__builtin_clear_padding");
    5383          639 :   else if (!clear_padding_type_may_have_padding_p (type))
    5384              :     ;
    5385          602 :   else if (TREE_CODE (type) == ARRAY_TYPE && buf.sz < 0)
    5386              :     {
    5387           48 :       tree sz = TYPE_SIZE_UNIT (type);
    5388           48 :       tree elttype = type;
    5389              :       /* Only supports C/C++ VLAs and flattens all the VLA levels.  */
    5390           48 :       while (TREE_CODE (elttype) == ARRAY_TYPE
    5391          144 :              && int_size_in_bytes (elttype) < 0)
    5392           96 :         elttype = TREE_TYPE (elttype);
    5393           48 :       HOST_WIDE_INT eltsz = int_size_in_bytes (elttype);
    5394           48 :       gcc_assert (eltsz >= 0);
    5395           48 :       if (eltsz)
    5396              :         {
    5397           48 :           buf.base = create_tmp_var (build_pointer_type (elttype));
    5398           48 :           tree end = make_ssa_name (TREE_TYPE (buf.base));
    5399           48 :           gimple *g = gimple_build_assign (buf.base, ptr);
    5400           48 :           gimple_set_location (g, loc);
    5401           48 :           gsi_insert_before (gsi, g, GSI_SAME_STMT);
    5402           48 :           g = gimple_build_assign (end, POINTER_PLUS_EXPR, buf.base, sz);
    5403           48 :           gimple_set_location (g, loc);
    5404           48 :           gsi_insert_before (gsi, g, GSI_SAME_STMT);
    5405           48 :           buf.sz = eltsz;
    5406           48 :           buf.align = TYPE_ALIGN (elttype);
    5407           48 :           buf.alias_type = build_pointer_type (elttype);
    5408           48 :           clear_padding_emit_loop (&buf, elttype, end, for_auto_init);
    5409              :         }
    5410              :     }
    5411              :   else
    5412              :     {
    5413          554 :       if (!is_gimple_mem_ref_addr (buf.base))
    5414              :         {
    5415           28 :           buf.base = make_ssa_name (TREE_TYPE (ptr));
    5416           28 :           gimple *g = gimple_build_assign (buf.base, ptr);
    5417           28 :           gimple_set_location (g, loc);
    5418           28 :           gsi_insert_before (gsi, g, GSI_SAME_STMT);
    5419              :         }
    5420          554 :       buf.alias_type = build_pointer_type (type);
    5421          554 :       clear_padding_type (&buf, type, buf.sz, for_auto_init);
    5422          554 :       clear_padding_flush (&buf, true);
    5423              :     }
    5424              : 
    5425          640 :   gimple_stmt_iterator gsiprev2 = *gsi;
    5426          640 :   gsi_prev (&gsiprev2);
    5427          640 :   if (gsi_stmt (gsiprev) == gsi_stmt (gsiprev2))
    5428          126 :     gsi_replace (gsi, gimple_build_nop (), true);
    5429              :   else
    5430              :     {
    5431          514 :       gsi_remove (gsi, true);
    5432          514 :       *gsi = gsiprev2;
    5433              :     }
    5434          640 :   return true;
    5435              : }
    5436              : 
    5437              : /* Fold __builtin_constant_p builtin.  */
    5438              : 
    5439              : static bool
    5440       171070 : gimple_fold_builtin_constant_p (gimple_stmt_iterator *gsi)
    5441              : {
    5442       171070 :   gcall *call = as_a<gcall*>(gsi_stmt (*gsi));
    5443              : 
    5444       171070 :   if (gimple_call_num_args (call) != 1)
    5445              :     return false;
    5446              : 
    5447       171065 :   tree arg = gimple_call_arg (call, 0);
    5448       171065 :   tree result = fold_builtin_constant_p (arg);
    5449              : 
    5450              :   /* Resolve __builtin_constant_p.  If it hasn't been
    5451              :      folded to integer_one_node by now, it's fairly
    5452              :      certain that the value simply isn't constant.  */
    5453       340059 :   if (!result && fold_before_rtl_expansion_p ())
    5454            3 :     result = integer_zero_node;
    5455              : 
    5456       171065 :   if (!result)
    5457              :     return false;
    5458              : 
    5459         2074 :   gimplify_and_update_call_from_tree (gsi, result);
    5460         2074 :   return true;
    5461              : }
    5462              : 
    5463              : /* If va_list type is a simple pointer and nothing special is needed,
    5464              :    optimize __builtin_va_start (&ap, 0) into ap = __builtin_next_arg (0),
    5465              :    __builtin_va_end (&ap) out as NOP and __builtin_va_copy into a simple
    5466              :    pointer assignment.  Returns true if a change happened.  */
    5467              : 
    5468              : static bool
    5469       115363 : gimple_fold_builtin_stdarg (gimple_stmt_iterator *gsi, gcall *call)
    5470              : {
    5471              :   /* These shouldn't be folded before pass_stdarg.  */
    5472       115363 :   if (!fold_before_rtl_expansion_p ())
    5473              :     return false;
    5474              : 
    5475        10835 :   tree callee, lhs, rhs, cfun_va_list;
    5476        10835 :   bool va_list_simple_ptr;
    5477        10835 :   location_t loc = gimple_location (call);
    5478        10835 :   gimple *nstmt0, *nstmt;
    5479        10835 :   tree tlhs, oldvdef, newvdef;
    5480              : 
    5481        10835 :   callee = gimple_call_fndecl (call);
    5482              : 
    5483        10835 :   cfun_va_list = targetm.fn_abi_va_list (callee);
    5484        21670 :   va_list_simple_ptr = POINTER_TYPE_P (cfun_va_list)
    5485        10835 :                        && (TREE_TYPE (cfun_va_list) == void_type_node
    5486          434 :                            || TREE_TYPE (cfun_va_list) == char_type_node);
    5487              : 
    5488        10835 :   switch (DECL_FUNCTION_CODE (callee))
    5489              :     {
    5490         7019 :     case BUILT_IN_VA_START:
    5491         7019 :       if (!va_list_simple_ptr
    5492          172 :           || targetm.expand_builtin_va_start != NULL
    5493         7175 :           || !builtin_decl_explicit_p (BUILT_IN_NEXT_ARG))
    5494              :         return false;
    5495              : 
    5496          156 :       if (gimple_call_num_args (call) != 2)
    5497              :         return false;
    5498              : 
    5499          156 :       lhs = gimple_call_arg (call, 0);
    5500          156 :       if (!POINTER_TYPE_P (TREE_TYPE (lhs))
    5501          156 :           || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (lhs)))
    5502          156 :              != TYPE_MAIN_VARIANT (cfun_va_list))
    5503              :         return false;
    5504              :       /* Create `tlhs = __builtin_next_arg(0);`. */
    5505          156 :       tlhs = make_ssa_name (cfun_va_list);
    5506          156 :       nstmt0 = gimple_build_call (builtin_decl_explicit (BUILT_IN_NEXT_ARG), 1, integer_zero_node);
    5507          156 :       lhs = fold_build2 (MEM_REF, cfun_va_list, lhs, build_zero_cst (TREE_TYPE (lhs)));
    5508          156 :       gimple_call_set_lhs (nstmt0, tlhs);
    5509          156 :       gimple_set_location (nstmt0, loc);
    5510          156 :       gimple_move_vops (nstmt0, call);
    5511          156 :       gsi_replace (gsi, nstmt0, false);
    5512          156 :       oldvdef = gimple_vdef (nstmt0);
    5513          156 :       newvdef = make_ssa_name (gimple_vop (cfun), nstmt0);
    5514          156 :       gimple_set_vdef (nstmt0, newvdef);
    5515              : 
    5516              :       /* Create `*lhs = tlhs;`.  */
    5517          156 :       nstmt = gimple_build_assign (lhs, tlhs);
    5518          156 :       gimple_set_location (nstmt, loc);
    5519          156 :       gimple_set_vuse (nstmt, newvdef);
    5520          156 :       gimple_set_vdef (nstmt, oldvdef);
    5521          156 :       SSA_NAME_DEF_STMT (oldvdef) = nstmt;
    5522          156 :       gsi_insert_after (gsi, nstmt, GSI_NEW_STMT);
    5523              : 
    5524          156 :       if (dump_file && (dump_flags & TDF_DETAILS))
    5525              :         {
    5526            0 :           fprintf (dump_file, "Simplified\n  ");
    5527            0 :           print_gimple_stmt (dump_file, call, 0, dump_flags);
    5528            0 :           fprintf (dump_file, "into\n  ");
    5529            0 :           print_gimple_stmt (dump_file, nstmt0, 0, dump_flags);
    5530            0 :           fprintf (dump_file, "  ");
    5531            0 :           print_gimple_stmt (dump_file, nstmt, 0, dump_flags);
    5532              :         }
    5533              :       return true;
    5534              : 
    5535          244 :     case BUILT_IN_VA_COPY:
    5536          244 :       if (!va_list_simple_ptr)
    5537              :         return false;
    5538              : 
    5539           47 :       if (gimple_call_num_args (call) != 2)
    5540              :         return false;
    5541              : 
    5542           47 :       lhs = gimple_call_arg (call, 0);
    5543           47 :       if (!POINTER_TYPE_P (TREE_TYPE (lhs))
    5544           47 :           || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (lhs)))
    5545           47 :              != TYPE_MAIN_VARIANT (cfun_va_list))
    5546              :         return false;
    5547           47 :       rhs = gimple_call_arg (call, 1);
    5548           47 :       if (TYPE_MAIN_VARIANT (TREE_TYPE (rhs))
    5549           47 :           != TYPE_MAIN_VARIANT (cfun_va_list))
    5550              :         return false;
    5551              : 
    5552           47 :       lhs = fold_build2 (MEM_REF, cfun_va_list, lhs, build_zero_cst (TREE_TYPE (lhs)));
    5553           47 :       nstmt = gimple_build_assign (lhs, rhs);
    5554           47 :       gimple_set_location (nstmt, loc);
    5555           47 :       gimple_move_vops (nstmt, call);
    5556           47 :       gsi_replace (gsi, nstmt, false);
    5557              : 
    5558           47 :       if (dump_file && (dump_flags & TDF_DETAILS))
    5559              :         {
    5560            0 :           fprintf (dump_file, "Simplified\n  ");
    5561            0 :           print_gimple_stmt (dump_file, call, 0, dump_flags);
    5562            0 :           fprintf (dump_file, "into\n  ");
    5563            0 :           print_gimple_stmt (dump_file, nstmt, 0, dump_flags);
    5564              :         }
    5565              :       return true;
    5566              : 
    5567         3572 :     case BUILT_IN_VA_END:
    5568              :       /* No effect, so the statement will be deleted.  */
    5569         3572 :       if (dump_file && (dump_flags & TDF_DETAILS))
    5570              :         {
    5571            0 :           fprintf (dump_file, "Removed\n  ");
    5572            0 :           print_gimple_stmt (dump_file, call, 0, dump_flags);
    5573              :         }
    5574         3572 :       unlink_stmt_vdef (call);
    5575         3572 :       release_defs (call);
    5576         3572 :       gsi_replace (gsi, gimple_build_nop (), false);
    5577         3572 :       return true;
    5578              : 
    5579            0 :     default:
    5580            0 :       gcc_unreachable ();
    5581              :     }
    5582              : }
    5583              : 
    5584              : /* Fold __builtin_call_{code_address,static_chain} builtins.  This handles
    5585              :    only the trivial left-over cases not processed in tree-nested.cc.  */
    5586              : 
    5587              : static bool
    5588            2 : gimple_fold_builtin_call_info (gimple_stmt_iterator *gsi,
    5589              :                                enum built_in_function fcode)
    5590              : {
    5591            2 :   gcall *stmt = as_a <gcall *>(gsi_stmt (*gsi));
    5592            2 :   tree arg = gimple_call_arg (stmt, 0);
    5593              : 
    5594              :   /* The error will be emitted in builtins.cc.  */
    5595            2 :   if (TREE_CODE (arg) != ADDR_EXPR
    5596            2 :       || FUNCTION_DECL != TREE_CODE (TREE_OPERAND (arg, 0)))
    5597              :     return false;
    5598              : 
    5599              :   /* The case with static chain is handled in tree-nested.cc.  */
    5600            2 :   gcc_assert (!DECL_STATIC_CHAIN (TREE_OPERAND (arg, 0)));
    5601              : 
    5602            2 :   if (fcode == BUILT_IN_CALL_STATIC_CHAIN)
    5603            1 :     replace_call_with_value (gsi, null_pointer_node);
    5604              :   else
    5605            1 :     replace_call_with_value (gsi, arg);
    5606              : 
    5607              :   return true;
    5608              : }
    5609              : 
    5610              : 
    5611              : /* Fold the non-target builtin at *GSI and return whether any simplification
    5612              :    was made.  */
    5613              : 
    5614              : static bool
    5615      9760457 : gimple_fold_builtin (gimple_stmt_iterator *gsi)
    5616              : {
    5617      9760457 :   gcall *stmt = as_a <gcall *>(gsi_stmt (*gsi));
    5618      9760457 :   tree callee = gimple_call_fndecl (stmt);
    5619              : 
    5620              :   /* Give up for always_inline inline builtins until they are
    5621              :      inlined.  */
    5622      9760457 :   if (avoid_folding_inline_builtin (callee))
    5623              :     return false;
    5624              : 
    5625      9759280 :   unsigned n = gimple_call_num_args (stmt);
    5626      9759280 :   enum built_in_function fcode = DECL_FUNCTION_CODE (callee);
    5627      9759280 :   switch (fcode)
    5628              :     {
    5629       115363 :     case BUILT_IN_VA_START:
    5630       115363 :     case BUILT_IN_VA_END:
    5631       115363 :     case BUILT_IN_VA_COPY:
    5632       115363 :       return gimple_fold_builtin_stdarg (gsi, stmt);
    5633          149 :     case BUILT_IN_BCMP:
    5634          149 :       return gimple_fold_builtin_bcmp (gsi);
    5635          367 :     case BUILT_IN_BCOPY:
    5636          367 :       return gimple_fold_builtin_bcopy (gsi);
    5637          250 :     case BUILT_IN_BZERO:
    5638          250 :       return gimple_fold_builtin_bzero (gsi);
    5639              : 
    5640       329106 :     case BUILT_IN_MEMSET:
    5641       329106 :       return gimple_fold_builtin_memset (gsi,
    5642              :                                          gimple_call_arg (stmt, 1),
    5643       329106 :                                          gimple_call_arg (stmt, 2));
    5644        10287 :     case BUILT_IN_MEMPCPY:
    5645        10287 :       if (gimple_fold_builtin_memory_op (gsi, gimple_call_arg (stmt, 0),
    5646              :                                             gimple_call_arg (stmt, 1), fcode))
    5647              :         return true;
    5648         9545 :       return gimple_fold_builtin_mempcpy (gsi);
    5649      1027425 :     case BUILT_IN_MEMCPY:
    5650      1027425 :     case BUILT_IN_MEMMOVE:
    5651      1027425 :       return gimple_fold_builtin_memory_op (gsi, gimple_call_arg (stmt, 0),
    5652      1027425 :                                             gimple_call_arg (stmt, 1), fcode);
    5653         4471 :     case BUILT_IN_SPRINTF_CHK:
    5654         4471 :     case BUILT_IN_VSPRINTF_CHK:
    5655         4471 :       return gimple_fold_builtin_sprintf_chk (gsi, fcode);
    5656         1493 :     case BUILT_IN_STRCAT_CHK:
    5657         1493 :       return gimple_fold_builtin_strcat_chk (gsi);
    5658         1116 :     case BUILT_IN_STRNCAT_CHK:
    5659         1116 :       return gimple_fold_builtin_strncat_chk (gsi);
    5660       139604 :     case BUILT_IN_STRLEN:
    5661       139604 :       return gimple_fold_builtin_strlen (gsi);
    5662        26263 :     case BUILT_IN_STRCPY:
    5663        26263 :       return gimple_fold_builtin_strcpy (gsi,
    5664              :                                          gimple_call_arg (stmt, 0),
    5665        26263 :                                          gimple_call_arg (stmt, 1));
    5666        17276 :     case BUILT_IN_STRNCPY:
    5667        17276 :       return gimple_fold_builtin_strncpy (gsi,
    5668              :                                           gimple_call_arg (stmt, 0),
    5669              :                                           gimple_call_arg (stmt, 1),
    5670        17276 :                                           gimple_call_arg (stmt, 2));
    5671         7464 :     case BUILT_IN_STRCAT:
    5672         7464 :       return gimple_fold_builtin_strcat (gsi, gimple_call_arg (stmt, 0),
    5673         7464 :                                          gimple_call_arg (stmt, 1));
    5674         6786 :     case BUILT_IN_STRNCAT:
    5675         6786 :       return gimple_fold_builtin_strncat (gsi);
    5676         4743 :     case BUILT_IN_INDEX:
    5677         4743 :     case BUILT_IN_STRCHR:
    5678         4743 :       return gimple_fold_builtin_strchr (gsi, false);
    5679          733 :     case BUILT_IN_RINDEX:
    5680          733 :     case BUILT_IN_STRRCHR:
    5681          733 :       return gimple_fold_builtin_strchr (gsi, true);
    5682         4347 :     case BUILT_IN_STRSTR:
    5683         4347 :       return gimple_fold_builtin_strstr (gsi);
    5684      1251175 :     case BUILT_IN_STRCMP:
    5685      1251175 :     case BUILT_IN_STRCMP_EQ:
    5686      1251175 :     case BUILT_IN_STRCASECMP:
    5687      1251175 :     case BUILT_IN_STRNCMP:
    5688      1251175 :     case BUILT_IN_STRNCMP_EQ:
    5689      1251175 :     case BUILT_IN_STRNCASECMP:
    5690      1251175 :       return gimple_fold_builtin_string_compare (gsi);
    5691        29511 :     case BUILT_IN_MEMCHR:
    5692        29511 :       return gimple_fold_builtin_memchr (gsi);
    5693        20794 :     case BUILT_IN_FPUTS:
    5694        20794 :       return gimple_fold_builtin_fputs (gsi, gimple_call_arg (stmt, 0),
    5695        20794 :                                         gimple_call_arg (stmt, 1), false);
    5696           43 :     case BUILT_IN_FPUTS_UNLOCKED:
    5697           43 :       return gimple_fold_builtin_fputs (gsi, gimple_call_arg (stmt, 0),
    5698           43 :                                         gimple_call_arg (stmt, 1), true);
    5699        41128 :     case BUILT_IN_FWRITE:
    5700        41128 :       return gimple_fold_builtin_fwrite (gsi, false);
    5701          267 :     case BUILT_IN_FWRITE_UNLOCKED:
    5702          267 :       return gimple_fold_builtin_fwrite (gsi, true);
    5703        25701 :     case BUILT_IN_MEMCPY_CHK:
    5704        25701 :     case BUILT_IN_MEMPCPY_CHK:
    5705        25701 :     case BUILT_IN_MEMMOVE_CHK:
    5706        25701 :     case BUILT_IN_MEMSET_CHK:
    5707        25701 :       return gimple_fold_builtin_memory_chk (gsi,
    5708              :                                              gimple_call_arg (stmt, 0),
    5709              :                                              gimple_call_arg (stmt, 1),
    5710              :                                              gimple_call_arg (stmt, 2),
    5711              :                                              gimple_call_arg (stmt, 3),
    5712        25701 :                                              fcode);
    5713         3674 :     case BUILT_IN_STPCPY:
    5714         3674 :       return gimple_fold_builtin_stpcpy (gsi);
    5715         2566 :     case BUILT_IN_STRCPY_CHK:
    5716         2566 :     case BUILT_IN_STPCPY_CHK:
    5717         2566 :       return gimple_fold_builtin_stxcpy_chk (gsi,
    5718              :                                              gimple_call_arg (stmt, 0),
    5719              :                                              gimple_call_arg (stmt, 1),
    5720              :                                              gimple_call_arg (stmt, 2),
    5721         2566 :                                              fcode);
    5722         2721 :     case BUILT_IN_STRNCPY_CHK:
    5723         2721 :     case BUILT_IN_STPNCPY_CHK:
    5724         2721 :       return gimple_fold_builtin_stxncpy_chk (gsi,
    5725              :                                               gimple_call_arg (stmt, 0),
    5726              :                                               gimple_call_arg (stmt, 1),
    5727              :                                               gimple_call_arg (stmt, 2),
    5728              :                                               gimple_call_arg (stmt, 3),
    5729         2721 :                                               fcode);
    5730         2359 :     case BUILT_IN_SNPRINTF_CHK:
    5731         2359 :     case BUILT_IN_VSNPRINTF_CHK:
    5732         2359 :       return gimple_fold_builtin_snprintf_chk (gsi, fcode);
    5733              : 
    5734       867237 :     case BUILT_IN_FPRINTF:
    5735       867237 :     case BUILT_IN_FPRINTF_UNLOCKED:
    5736       867237 :     case BUILT_IN_VFPRINTF:
    5737       867237 :       if (n == 2 || n == 3)
    5738        95328 :         return gimple_fold_builtin_fprintf (gsi,
    5739              :                                             gimple_call_arg (stmt, 0),
    5740              :                                             gimple_call_arg (stmt, 1),
    5741              :                                             n == 3
    5742        42659 :                                             ? gimple_call_arg (stmt, 2)
    5743              :                                             : NULL_TREE,
    5744        52669 :                                             fcode);
    5745              :       break;
    5746         2229 :     case BUILT_IN_FPRINTF_CHK:
    5747         2229 :     case BUILT_IN_VFPRINTF_CHK:
    5748         2229 :       if (n == 3 || n == 4)
    5749         3814 :         return gimple_fold_builtin_fprintf (gsi,
    5750              :                                             gimple_call_arg (stmt, 0),
    5751              :                                             gimple_call_arg (stmt, 2),
    5752              :                                             n == 4
    5753         1744 :                                             ? gimple_call_arg (stmt, 3)
    5754              :                                             : NULL_TREE,
    5755         2070 :                                             fcode);
    5756              :       break;
    5757       201495 :     case BUILT_IN_PRINTF:
    5758       201495 :     case BUILT_IN_PRINTF_UNLOCKED:
    5759       201495 :     case BUILT_IN_VPRINTF:
    5760       201495 :       if (n == 1 || n == 2)
    5761       238766 :         return gimple_fold_builtin_printf (gsi, gimple_call_arg (stmt, 0),
    5762              :                                            n == 2
    5763       114820 :                                            ? gimple_call_arg (stmt, 1)
    5764       123946 :                                            : NULL_TREE, fcode);
    5765              :       break;
    5766         2273 :     case BUILT_IN_PRINTF_CHK:
    5767         2273 :     case BUILT_IN_VPRINTF_CHK:
    5768         2273 :       if (n == 2 || n == 3)
    5769         3893 :         return gimple_fold_builtin_printf (gsi, gimple_call_arg (stmt, 1),
    5770              :                                            n == 3
    5771         1771 :                                            ? gimple_call_arg (stmt, 2)
    5772         2122 :                                            : NULL_TREE, fcode);
    5773              :       break;
    5774         2866 :     case BUILT_IN_ACC_ON_DEVICE:
    5775         2866 :       return gimple_fold_builtin_acc_on_device (gsi,
    5776         2866 :                                                 gimple_call_arg (stmt, 0));
    5777          266 :     case BUILT_IN_OMP_IS_INITIAL_DEVICE:
    5778          266 :       return gimple_fold_builtin_omp_is_initial_device (gsi);
    5779              : 
    5780          103 :     case BUILT_IN_OMP_GET_INITIAL_DEVICE:
    5781          103 :       return gimple_fold_builtin_omp_get_initial_device (gsi);
    5782              : 
    5783          340 :     case BUILT_IN_OMP_GET_NUM_DEVICES:
    5784          340 :       return gimple_fold_builtin_omp_get_num_devices (gsi);
    5785              : 
    5786        51060 :     case BUILT_IN_REALLOC:
    5787        51060 :       return gimple_fold_builtin_realloc (gsi);
    5788              : 
    5789          640 :     case BUILT_IN_CLEAR_PADDING:
    5790          640 :       return gimple_fold_builtin_clear_padding (gsi);
    5791              : 
    5792       171070 :     case BUILT_IN_CONSTANT_P:
    5793       171070 :       return gimple_fold_builtin_constant_p (gsi);
    5794              : 
    5795            2 :     case BUILT_IN_CALL_CODE_ADDRESS:
    5796            2 :     case BUILT_IN_CALL_STATIC_CHAIN:
    5797            2 :       return gimple_fold_builtin_call_info (gsi, fcode);
    5798              : 
    5799      6274944 :     default:;
    5800              :     }
    5801              : 
    5802              :   /* Try the generic builtin folder.  */
    5803      6274944 :   bool ignore = (gimple_call_lhs (stmt) == NULL);
    5804      6274944 :   tree result = fold_call_stmt (stmt, ignore);
    5805      6274944 :   if (result)
    5806              :     {
    5807         5158 :       if (ignore)
    5808         1288 :         STRIP_NOPS (result);
    5809              :       else
    5810         3870 :         result = fold_convert (gimple_call_return_type (stmt), result);
    5811         5158 :       gimplify_and_update_call_from_tree (gsi, result);
    5812         5158 :       return true;
    5813              :     }
    5814              : 
    5815              :   return false;
    5816              : }
    5817              : 
    5818              : /* Transform IFN_GOACC_DIM_SIZE and IFN_GOACC_DIM_POS internal
    5819              :    function calls to constants, where possible.  */
    5820              : 
    5821              : static tree
    5822        20589 : fold_internal_goacc_dim (const gimple *call)
    5823              : {
    5824        20589 :   int axis = oacc_get_ifn_dim_arg (call);
    5825        20589 :   int size = oacc_get_fn_dim_size (current_function_decl, axis);
    5826        20589 :   tree result = NULL_TREE;
    5827        20589 :   tree type = TREE_TYPE (gimple_call_lhs (call));
    5828              : 
    5829        20589 :   switch (gimple_call_internal_fn (call))
    5830              :     {
    5831         8915 :     case IFN_GOACC_DIM_POS:
    5832              :       /* If the size is 1, we know the answer.  */
    5833         8915 :       if (size == 1)
    5834         8915 :         result = build_int_cst (type, 0);
    5835              :       break;
    5836        11674 :     case IFN_GOACC_DIM_SIZE:
    5837              :       /* If the size is not dynamic, we know the answer.  */
    5838        11674 :       if (size)
    5839        11674 :         result = build_int_cst (type, size);
    5840              :       break;
    5841              :     default:
    5842              :       break;
    5843              :     }
    5844              : 
    5845        20589 :   return result;
    5846              : }
    5847              : 
    5848              : /* Return true if stmt is __atomic_compare_exchange_N call which is suitable
    5849              :    for conversion into ATOMIC_COMPARE_EXCHANGE if the second argument is
    5850              :    &var where var is only addressable because of such calls.  */
    5851              : 
    5852              : bool
    5853     61292049 : optimize_atomic_compare_exchange_p (gimple *stmt)
    5854              : {
    5855     61292049 :   if (gimple_call_num_args (stmt) != 6
    5856      1688134 :       || !flag_inline_atomics
    5857      1688134 :       || !optimize
    5858      1688134 :       || sanitize_flags_p (SANITIZE_THREAD | SANITIZE_ADDRESS)
    5859      1688053 :       || !gimple_call_builtin_p (stmt, BUILT_IN_NORMAL)
    5860      1134214 :       || !gimple_vdef (stmt)
    5861     62303935 :       || !gimple_vuse (stmt))
    5862              :     return false;
    5863              : 
    5864      1011886 :   tree fndecl = gimple_call_fndecl (stmt);
    5865      1011886 :   switch (DECL_FUNCTION_CODE (fndecl))
    5866              :     {
    5867        52030 :     case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
    5868        52030 :     case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
    5869        52030 :     case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
    5870        52030 :     case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
    5871        52030 :     case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
    5872        52030 :       break;
    5873              :     default:
    5874              :       return false;
    5875              :     }
    5876              : 
    5877        52030 :   tree expected = gimple_call_arg (stmt, 1);
    5878        52030 :   if (TREE_CODE (expected) != ADDR_EXPR
    5879        52030 :       || !SSA_VAR_P (TREE_OPERAND (expected, 0)))
    5880              :     return false;
    5881              : 
    5882        49642 :   tree etype = TREE_TYPE (TREE_OPERAND (expected, 0));
    5883        49642 :   if (!is_gimple_reg_type (etype)
    5884        49144 :       || !auto_var_in_fn_p (TREE_OPERAND (expected, 0), current_function_decl)
    5885        46746 :       || TREE_THIS_VOLATILE (etype)
    5886        46746 :       || VECTOR_TYPE_P (etype)
    5887              :       || TREE_CODE (etype) == COMPLEX_TYPE
    5888              :       /* Don't optimize floating point expected vars, VIEW_CONVERT_EXPRs
    5889              :          might not preserve all the bits.  See PR71716.  */
    5890              :       || SCALAR_FLOAT_TYPE_P (etype)
    5891        67726 :       || maybe_ne (TYPE_PRECISION (etype),
    5892        36168 :                    GET_MODE_BITSIZE (TYPE_MODE (etype))))
    5893              :     return false;
    5894              : 
    5895        11660 :   tree weak = gimple_call_arg (stmt, 3);
    5896        11660 :   if (!integer_zerop (weak) && !integer_onep (weak))
    5897              :     return false;
    5898              : 
    5899        11660 :   tree parmt = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
    5900        11660 :   tree itype = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (parmt)));
    5901        11660 :   machine_mode mode = TYPE_MODE (itype);
    5902              : 
    5903        11660 :   if (direct_optab_handler (atomic_compare_and_swap_optab, mode)
    5904              :       == CODE_FOR_nothing
    5905        11660 :       && optab_handler (sync_compare_and_swap_optab, mode) == CODE_FOR_nothing)
    5906              :     return false;
    5907              : 
    5908        23320 :   if (maybe_ne (int_size_in_bytes (etype), GET_MODE_SIZE (mode)))
    5909            0 :     return false;
    5910              : 
    5911              :   return true;
    5912              : }
    5913              : 
    5914              : /* Fold
    5915              :      r = __atomic_compare_exchange_N (p, &e, d, w, s, f);
    5916              :    into
    5917              :      _Complex uintN_t t = ATOMIC_COMPARE_EXCHANGE (p, e, d, w * 256 + N, s, f);
    5918              :      i = IMAGPART_EXPR <t>;
    5919              :      r = (_Bool) i;
    5920              :      e = REALPART_EXPR <t>;  */
    5921              : 
    5922              : void
    5923         5787 : fold_builtin_atomic_compare_exchange (gimple_stmt_iterator *gsi)
    5924              : {
    5925         5787 :   gimple *stmt = gsi_stmt (*gsi);
    5926         5787 :   tree fndecl = gimple_call_fndecl (stmt);
    5927         5787 :   tree parmt = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
    5928         5787 :   tree itype = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (parmt)));
    5929         5787 :   tree ctype = build_complex_type (itype);
    5930         5787 :   tree expected = TREE_OPERAND (gimple_call_arg (stmt, 1), 0);
    5931         5787 :   bool throws = false;
    5932         5787 :   edge e = NULL;
    5933         5787 :   gassign *g = gimple_build_assign (make_ssa_name (TREE_TYPE (expected)),
    5934              :                                     expected);
    5935         5787 :   gsi_insert_before (gsi, g, GSI_SAME_STMT);
    5936         5787 :   gimple_stmt_iterator gsiret = gsi_for_stmt (g);
    5937         5787 :   if (!useless_type_conversion_p (itype, TREE_TYPE (expected)))
    5938              :     {
    5939         2651 :       g = gimple_build_assign (make_ssa_name (itype), VIEW_CONVERT_EXPR,
    5940              :                                build1 (VIEW_CONVERT_EXPR, itype,
    5941              :                                        gimple_assign_lhs (g)));
    5942         2651 :       gsi_insert_before (gsi, g, GSI_SAME_STMT);
    5943              :     }
    5944         5787 :   int flag = (integer_onep (gimple_call_arg (stmt, 3)) ? 256 : 0)
    5945        11119 :              + int_size_in_bytes (itype);
    5946         5787 :   gcall *c = gimple_build_call_internal (IFN_ATOMIC_COMPARE_EXCHANGE, 6,
    5947              :                                          gimple_call_arg (stmt, 0),
    5948              :                                          gimple_assign_lhs (g),
    5949              :                                          gimple_call_arg (stmt, 2),
    5950              :                                          build_int_cst (integer_type_node,
    5951         5787 :                                                         flag),
    5952              :                                          gimple_call_arg (stmt, 4),
    5953              :                                          gimple_call_arg (stmt, 5));
    5954         5787 :   tree lhs = make_ssa_name (ctype);
    5955         5787 :   gimple_call_set_lhs (c, lhs);
    5956         5787 :   gimple_move_vops (c, stmt);
    5957         5787 :   tree oldlhs = gimple_call_lhs (stmt);
    5958         5787 :   if (stmt_can_throw_internal (cfun, stmt))
    5959              :     {
    5960          203 :       throws = true;
    5961          203 :       e = find_fallthru_edge (gsi_bb (*gsi)->succs);
    5962              :     }
    5963         5787 :   gimple_call_set_nothrow (c,
    5964         5787 :                            gimple_call_nothrow_p (as_a <gcall *> (stmt)));
    5965         5787 :   gimple_call_set_lhs (stmt, NULL_TREE);
    5966         5787 :   gsi_replace (gsi, c, true);
    5967         5787 :   if (oldlhs)
    5968              :     {
    5969         5740 :       g = gimple_build_assign (make_ssa_name (itype), IMAGPART_EXPR,
    5970              :                                build1 (IMAGPART_EXPR, itype, lhs));
    5971         5740 :       if (throws)
    5972              :         {
    5973          197 :           gsi_insert_on_edge_immediate (e, g);
    5974          197 :           *gsi = gsi_for_stmt (g);
    5975              :         }
    5976              :       else
    5977         5543 :         gsi_insert_after (gsi, g, GSI_NEW_STMT);
    5978         5740 :       g = gimple_build_assign (oldlhs, NOP_EXPR, gimple_assign_lhs (g));
    5979         5740 :       gsi_insert_after (gsi, g, GSI_NEW_STMT);
    5980              :     }
    5981         5787 :   g = gimple_build_assign (make_ssa_name (itype), REALPART_EXPR,
    5982              :                            build1 (REALPART_EXPR, itype, lhs));
    5983         5787 :   if (throws && oldlhs == NULL_TREE)
    5984              :     {
    5985            6 :       gsi_insert_on_edge_immediate (e, g);
    5986            6 :       *gsi = gsi_for_stmt (g);
    5987              :     }
    5988              :   else
    5989         5781 :     gsi_insert_after (gsi, g, GSI_NEW_STMT);
    5990         5787 :   if (!useless_type_conversion_p (TREE_TYPE (expected), itype))
    5991              :     {
    5992         2651 :       g = gimple_build_assign (make_ssa_name (TREE_TYPE (expected)),
    5993              :                                VIEW_CONVERT_EXPR,
    5994         2651 :                                build1 (VIEW_CONVERT_EXPR, TREE_TYPE (expected),
    5995              :                                        gimple_assign_lhs (g)));
    5996         2651 :       gsi_insert_after (gsi, g, GSI_NEW_STMT);
    5997              :     }
    5998         5787 :   g = gimple_build_assign (expected, SSA_NAME, gimple_assign_lhs (g));
    5999         5787 :   gsi_insert_after (gsi, g, GSI_NEW_STMT);
    6000         5787 :   *gsi = gsiret;
    6001         5787 : }
    6002              : 
    6003              : /* Return true if ARG0 CODE ARG1 in infinite signed precision operation
    6004              :    doesn't fit into TYPE.  The test for overflow should be regardless of
    6005              :    -fwrapv, and even for unsigned types.  */
    6006              : 
    6007              : bool
    6008       452714 : arith_overflowed_p (enum tree_code code, const_tree type,
    6009              :                     const_tree arg0, const_tree arg1)
    6010              : {
    6011       452714 :   widest2_int warg0 = widest2_int_cst (arg0);
    6012       452714 :   widest2_int warg1 = widest2_int_cst (arg1);
    6013       452714 :   widest2_int wres;
    6014       452714 :   switch (code)
    6015              :     {
    6016        98449 :     case PLUS_EXPR: wres = wi::add (warg0, warg1); break;
    6017       116111 :     case MINUS_EXPR: wres = wi::sub (warg0, warg1); break;
    6018       239605 :     case MULT_EXPR: wres = wi::mul (warg0, warg1); break;
    6019            0 :     default: gcc_unreachable ();
    6020              :     }
    6021       452714 :   signop sign = TYPE_SIGN (type);
    6022       452714 :   if (sign == UNSIGNED && wi::neg_p (wres))
    6023              :     return true;
    6024       380773 :   return wi::min_precision (wres, sign) > TYPE_PRECISION (type);
    6025       452854 : }
    6026              : 
    6027              : /* Mask state for partial load/store operations (mask and length).  */
    6028              : enum mask_load_store_state {
    6029              :   MASK_ALL_INACTIVE,  /* All lanes/elements are inactive (can be elided).  */
    6030              :   MASK_ALL_ACTIVE,    /* All lanes/elements are active (unconditional).  */
    6031              :   MASK_UNKNOWN
    6032              : };
    6033              : 
    6034              : /* Check the mask/length state of IFN_{MASK,LEN,MASK_LEN}_LOAD/STORE call CALL.
    6035              :    Returns whether all elements are active, all inactive, or mixed.
    6036              :    VECTYPE is the vector type of the operation.  */
    6037              : 
    6038              : static enum mask_load_store_state
    6039         8699 : partial_load_store_mask_state (gcall *call, tree vectype)
    6040              : {
    6041         8699 :   internal_fn ifn = gimple_call_internal_fn (call);
    6042         8699 :   int mask_index = internal_fn_mask_index (ifn);
    6043         8699 :   int len_index = internal_fn_len_index (ifn);
    6044              : 
    6045              :   /* Extract length and mask arguments up front.  */
    6046         8699 :   tree len = len_index != -1 ? gimple_call_arg (call, len_index) : NULL_TREE;
    6047            0 :   tree bias = len ? gimple_call_arg (call, len_index + 1) : NULL_TREE;
    6048         8699 :   tree mask = mask_index != -1 ? gimple_call_arg (call, mask_index) : NULL_TREE;
    6049              : 
    6050        17398 :   poly_int64 nelts = GET_MODE_NUNITS (TYPE_MODE (vectype));
    6051              : 
    6052         8699 :   poly_widest_int wlen = -1;
    6053         8699 :   bool full_length_p = !len;  /* No length means full length.  */
    6054              : 
    6055              :   /* Compute effective length.  */
    6056         8699 :   if (len && poly_int_tree_p (len))
    6057              :     {
    6058            0 :       gcc_assert (TREE_CODE (bias) == INTEGER_CST);
    6059            0 :       wlen = wi::to_poly_widest (len) - wi::to_widest (bias);
    6060              : 
    6061            0 :       if (known_eq (wlen, 0))
    6062              :         return MASK_ALL_INACTIVE;
    6063              : 
    6064            0 :       if (known_eq (wlen, nelts))
    6065              :         full_length_p = true;
    6066              :       else
    6067         8699 :         full_length_p = false;
    6068              :     }
    6069              : 
    6070              :   /* Check mask for early return cases.  */
    6071         8699 :   if (mask)
    6072              :     {
    6073         8699 :       if (integer_zerop (mask))
    6074              :         return MASK_ALL_INACTIVE;
    6075              : 
    6076         8684 :       if (full_length_p && integer_all_onesp (mask))
    6077              :         return MASK_ALL_ACTIVE;
    6078              :     }
    6079            0 :   else if (full_length_p)
    6080              :     /* No mask and full length means all active.  */
    6081              :     return MASK_ALL_ACTIVE;
    6082              : 
    6083              :   /* For VLA vectors, we can't do much more.  */
    6084         8534 :   if (!nelts.is_constant ())
    6085              :     return MASK_UNKNOWN;
    6086              : 
    6087              :   /* Same for VLS vectors with non-constant mask.  */
    6088         8534 :   if (mask && TREE_CODE (mask) != VECTOR_CST)
    6089              :     return MASK_UNKNOWN;
    6090              : 
    6091              :   /* Check VLS vector elements.  */
    6092          504 :   gcc_assert (wlen.is_constant ());
    6093              : 
    6094          504 :   HOST_WIDE_INT active_len = wlen.to_constant ().to_shwi ();
    6095          504 :   if (active_len == -1)
    6096          504 :     active_len = nelts.to_constant ();
    6097              : 
    6098              :   /* Check if all elements in the active range match the mask.  */
    6099         1038 :   for (HOST_WIDE_INT i = 0; i < active_len; i++)
    6100              :     {
    6101         1038 :       bool elt_active = !mask || !integer_zerop (vector_cst_elt (mask, i));
    6102              :       if (!elt_active)
    6103              :         {
    6104              :           /* Found an inactive element.  Check if all are inactive.  */
    6105         1002 :           for (HOST_WIDE_INT j = 0; j < active_len; j++)
    6106         1002 :             if (!mask || !integer_zerop (vector_cst_elt (mask, j)))
    6107              :               return MASK_UNKNOWN;  /* Mixed state.  */
    6108              :           return MASK_ALL_INACTIVE;
    6109              :         }
    6110              :     }
    6111              : 
    6112              :   /* All elements in active range are active.  */
    6113            0 :   return full_length_p ? MASK_ALL_ACTIVE : MASK_UNKNOWN;
    6114         8699 : }
    6115              : 
    6116              : 
    6117              : /* If IFN_{MASK,LEN,MASK_LEN}_LOAD/STORE call CALL is unconditional
    6118              :    (all lanes active), return a MEM_REF for the memory it references.
    6119              :    Otherwise return NULL_TREE.  VECTYPE is the type of the memory vector.  */
    6120              : 
    6121              : static tree
    6122         4342 : gimple_fold_partial_load_store_mem_ref (gcall *call, tree vectype)
    6123              : {
    6124              :   /* Only fold if all lanes are active (unconditional).  */
    6125         4342 :   if (partial_load_store_mask_state (call, vectype) != MASK_ALL_ACTIVE)
    6126              :     return NULL_TREE;
    6127              : 
    6128           75 :   tree ptr = gimple_call_arg (call, 0);
    6129           75 :   tree alias_align = gimple_call_arg (call, 1);
    6130           75 :   if (!tree_fits_uhwi_p (alias_align))
    6131              :     return NULL_TREE;
    6132              : 
    6133           75 :   unsigned HOST_WIDE_INT align = tree_to_uhwi (alias_align);
    6134           75 :   if (TYPE_ALIGN (vectype) != align)
    6135           14 :     vectype = build_aligned_type (vectype, align);
    6136           75 :   tree offset = build_zero_cst (TREE_TYPE (alias_align));
    6137           75 :   return fold_build2 (MEM_REF, vectype, ptr, offset);
    6138              : }
    6139              : 
    6140              : /* Try to fold IFN_{MASK,LEN}_LOAD/STORE call CALL.  Return true on success.  */
    6141              : 
    6142              : static bool
    6143         4357 : gimple_fold_partial_load_store (gimple_stmt_iterator *gsi, gcall *call)
    6144              : {
    6145         4357 :   internal_fn ifn = gimple_call_internal_fn (call);
    6146         4357 :   tree lhs = gimple_call_lhs (call);
    6147         4357 :   bool is_load = (lhs != NULL_TREE);
    6148         4357 :   tree vectype;
    6149              : 
    6150         4357 :   if (is_load)
    6151         2185 :     vectype = TREE_TYPE (lhs);
    6152              :   else
    6153              :     {
    6154         2172 :       tree rhs = gimple_call_arg (call, internal_fn_stored_value_index (ifn));
    6155         2172 :       vectype = TREE_TYPE (rhs);
    6156              :     }
    6157              : 
    6158         4357 :   enum mask_load_store_state state
    6159         4357 :     = partial_load_store_mask_state (call, vectype);
    6160              : 
    6161              :   /* Handle all-inactive case.  */
    6162         4357 :   if (state == MASK_ALL_INACTIVE)
    6163              :     {
    6164           15 :       if (is_load)
    6165              :         {
    6166              :           /* Replace load with else value.  */
    6167           15 :           int else_index = internal_fn_else_index (ifn);
    6168           15 :           tree else_value = gimple_call_arg (call, else_index);
    6169           15 :           if (!is_gimple_reg (lhs))
    6170              :             {
    6171            0 :               if (!zerop (else_value))
    6172              :                 return false;
    6173            0 :               else_value = build_constructor (TREE_TYPE (lhs), NULL);
    6174              :             }
    6175           15 :           gassign *new_stmt = gimple_build_assign (lhs, else_value);
    6176           15 :           gimple_set_location (new_stmt, gimple_location (call));
    6177              :           /* When the lhs is an array for LANES version, then there is still
    6178              :              a store, move the vops from the old stmt to the new one.  */
    6179           15 :           if (!is_gimple_reg (lhs))
    6180            0 :             gimple_move_vops (new_stmt, call);
    6181           15 :           gsi_replace (gsi, new_stmt, false);
    6182           15 :           return true;
    6183              :         }
    6184              :       else
    6185              :         {
    6186              :           /* Remove inactive store altogether.  */
    6187            0 :           unlink_stmt_vdef (call);
    6188            0 :           release_defs (call);
    6189            0 :           gsi_replace (gsi, gimple_build_nop (), true);
    6190            0 :           return true;
    6191              :         }
    6192              :     }
    6193              : 
    6194              :   /* We cannot simplify a gather/scatter or load/store lanes further.  */
    6195         4342 :   if (internal_gather_scatter_fn_p (ifn)
    6196         4342 :       || TREE_CODE (vectype) == ARRAY_TYPE)
    6197              :     return false;
    6198              : 
    6199              :   /* Handle all-active case by folding to regular memory operation.  */
    6200         4342 :   if (tree mem_ref = gimple_fold_partial_load_store_mem_ref (call, vectype))
    6201              :     {
    6202           75 :       gassign *new_stmt;
    6203           75 :       if (is_load)
    6204           21 :         new_stmt = gimple_build_assign (lhs, mem_ref);
    6205              :       else
    6206              :         {
    6207           54 :           tree rhs
    6208           54 :             = gimple_call_arg (call, internal_fn_stored_value_index (ifn));
    6209           54 :           new_stmt = gimple_build_assign (mem_ref, rhs);
    6210              :         }
    6211              : 
    6212           75 :       gimple_set_location (new_stmt, gimple_location (call));
    6213           75 :       gimple_move_vops (new_stmt, call);
    6214           75 :       gsi_replace (gsi, new_stmt, false);
    6215           75 :       return true;
    6216              :     }
    6217              :   return false;
    6218              : }
    6219              : 
    6220              : /* Attempt to fold a call statement referenced by the statement iterator GSI.
    6221              :    The statement may be replaced by another statement, e.g., if the call
    6222              :    simplifies to a constant value. Return true if any changes were made.
    6223              :    It is assumed that the operands have been previously folded.  */
    6224              : 
    6225              : static bool
    6226     58355780 : gimple_fold_call (gimple_stmt_iterator *gsi, bool inplace)
    6227              : {
    6228     58355780 :   gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
    6229     58355780 :   tree callee;
    6230     58355780 :   bool changed = false;
    6231              : 
    6232              :   /* Check for virtual calls that became direct calls.  */
    6233     58355780 :   callee = gimple_call_fn (stmt);
    6234     58355780 :   if (callee && TREE_CODE (callee) == OBJ_TYPE_REF)
    6235              :     {
    6236       453671 :       if (gimple_call_addr_fndecl (OBJ_TYPE_REF_EXPR (callee)) != NULL_TREE)
    6237              :         {
    6238            6 :           if (dump_file && virtual_method_call_p (callee)
    6239          408 :               && !possible_polymorphic_call_target_p
    6240            6 :                     (callee, stmt, cgraph_node::get (gimple_call_addr_fndecl
    6241            6 :                                                      (OBJ_TYPE_REF_EXPR (callee)))))
    6242              :             {
    6243            0 :               fprintf (dump_file,
    6244              :                        "Type inheritance inconsistent devirtualization of ");
    6245            0 :               print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
    6246            0 :               fprintf (dump_file, " to ");
    6247            0 :               print_generic_expr (dump_file, callee, TDF_SLIM);
    6248            0 :               fprintf (dump_file, "\n");
    6249              :             }
    6250              : 
    6251          402 :           gimple_call_set_fn (stmt, OBJ_TYPE_REF_EXPR (callee));
    6252          402 :           changed = true;
    6253              :         }
    6254       453269 :       else if (flag_devirtualize && !inplace && virtual_method_call_p (callee))
    6255              :         {
    6256       448197 :           bool final;
    6257       448197 :           vec <cgraph_node *>targets
    6258       448197 :             = possible_polymorphic_call_targets (callee, stmt, &final);
    6259       450862 :           if (final && targets.length () <= 1 && dbg_cnt (devirt))
    6260              :             {
    6261         2029 :               tree lhs = gimple_call_lhs (stmt);
    6262         2029 :               if (dump_enabled_p ())
    6263              :                 {
    6264           34 :                   dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, stmt,
    6265              :                                    "folding virtual function call to %s\n",
    6266           34 :                                    targets.length () == 1
    6267           17 :                                    ? targets[0]->name ()
    6268              :                                    : "__builtin_unreachable");
    6269              :                 }
    6270         2029 :               if (targets.length () == 1)
    6271              :                 {
    6272         1990 :                   tree fndecl = targets[0]->decl;
    6273         1990 :                   gimple_call_set_fndecl (stmt, fndecl);
    6274         1990 :                   changed = true;
    6275              :                   /* If changing the call to __cxa_pure_virtual
    6276              :                      or similar noreturn function, adjust gimple_call_fntype
    6277              :                      too.  */
    6278         1990 :                   if (gimple_call_noreturn_p (stmt)
    6279           19 :                       && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fndecl)))
    6280           13 :                       && TYPE_ARG_TYPES (TREE_TYPE (fndecl))
    6281         2003 :                       && (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
    6282           13 :                           == void_type_node))
    6283           13 :                     gimple_call_set_fntype (stmt, TREE_TYPE (fndecl));
    6284              :                   /* If the call becomes noreturn, remove the lhs.  */
    6285         1990 :                   if (lhs
    6286         1669 :                       && gimple_call_noreturn_p (stmt)
    6287         2005 :                       && (VOID_TYPE_P (TREE_TYPE (gimple_call_fntype (stmt)))
    6288            6 :                           || should_remove_lhs_p (lhs)))
    6289              :                     {
    6290           12 :                       if (TREE_CODE (lhs) == SSA_NAME)
    6291              :                         {
    6292            0 :                           tree var = create_tmp_var (TREE_TYPE (lhs));
    6293            0 :                           tree def = get_or_create_ssa_default_def (cfun, var);
    6294            0 :                           gimple *new_stmt = gimple_build_assign (lhs, def);
    6295            0 :                           gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
    6296              :                         }
    6297           12 :                       gimple_call_set_lhs (stmt, NULL_TREE);
    6298              :                     }
    6299         1990 :                   maybe_remove_unused_call_args (cfun, stmt);
    6300              :                 }
    6301              :               else
    6302              :                 {
    6303           39 :                   location_t loc = gimple_location (stmt);
    6304           39 :                   gimple *new_stmt = gimple_build_builtin_unreachable (loc);
    6305           39 :                   gimple_call_set_ctrl_altering (new_stmt, false);
    6306              :                   /* If the call had a SSA name as lhs morph that into
    6307              :                      an uninitialized value.  */
    6308           39 :                   if (lhs && TREE_CODE (lhs) == SSA_NAME)
    6309              :                     {
    6310           12 :                       tree var = create_tmp_var (TREE_TYPE (lhs));
    6311           12 :                       SET_SSA_NAME_VAR_OR_IDENTIFIER (lhs, var);
    6312           12 :                       SSA_NAME_DEF_STMT (lhs) = gimple_build_nop ();
    6313           12 :                       set_ssa_default_def (cfun, var, lhs);
    6314              :                     }
    6315           39 :                   gimple_move_vops (new_stmt, stmt);
    6316           39 :                   gsi_replace (gsi, new_stmt, false);
    6317           39 :                   return true;
    6318              :                 }
    6319              :             }
    6320              :         }
    6321              :     }
    6322              : 
    6323              :   /* Check for indirect calls that became direct calls, and then
    6324              :      no longer require a static chain.  */
    6325     58355741 :   if (gimple_call_chain (stmt))
    6326              :     {
    6327       247497 :       tree fn = gimple_call_fndecl (stmt);
    6328       297152 :       if (fn && !DECL_STATIC_CHAIN (fn))
    6329              :         {
    6330         2081 :           gimple_call_set_chain (stmt, NULL);
    6331         2081 :           changed = true;
    6332              :         }
    6333              :     }
    6334              : 
    6335     58355741 :   if (inplace)
    6336              :     return changed;
    6337              : 
    6338              :   /* Don't constant fold functions which can change the control. */
    6339     58352637 :   if (gimple_call_ctrl_altering_p (stmt))
    6340              :     return changed;
    6341              : 
    6342              :   /* Check for builtins that CCP can handle using information not
    6343              :      available in the generic fold routines.  */
    6344     50474036 :   if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
    6345              :     {
    6346      9760457 :       if (gimple_fold_builtin (gsi))
    6347       214744 :         changed = true;
    6348              :     }
    6349     40713579 :   else if (gimple_call_builtin_p (stmt, BUILT_IN_MD))
    6350              :     {
    6351      1161820 :         changed |= targetm.gimple_fold_builtin (gsi);
    6352              :     }
    6353     39551759 :   else if (gimple_call_internal_p (stmt))
    6354              :     {
    6355      1995363 :       enum tree_code subcode = ERROR_MARK;
    6356      1995363 :       tree result = NULL_TREE;
    6357      1995363 :       bool cplx_result = false;
    6358      1995363 :       bool uaddc_usubc = false;
    6359      1995363 :       tree overflow = NULL_TREE;
    6360      1995363 :       switch (gimple_call_internal_fn (stmt))
    6361              :         {
    6362         1563 :         case IFN_ASSUME:
    6363              :           /* Remove .ASSUME calls during the last fold since it is no
    6364              :              longer needed.  */
    6365         1563 :           if (fold_before_rtl_expansion_p ())
    6366          204 :             replace_call_with_value (gsi, NULL_TREE);
    6367              :           break;
    6368       173808 :         case IFN_BUILTIN_EXPECT:
    6369       173808 :           result = fold_builtin_expect (gimple_location (stmt),
    6370              :                                         gimple_call_arg (stmt, 0),
    6371              :                                         gimple_call_arg (stmt, 1),
    6372              :                                         gimple_call_arg (stmt, 2),
    6373              :                                         NULL_TREE);
    6374       173808 :           break;
    6375         8881 :         case IFN_UBSAN_OBJECT_SIZE:
    6376         8881 :           {
    6377         8881 :             tree offset = gimple_call_arg (stmt, 1);
    6378         8881 :             tree objsize = gimple_call_arg (stmt, 2);
    6379         8881 :             if (integer_all_onesp (objsize)
    6380         8881 :                 || (TREE_CODE (offset) == INTEGER_CST
    6381         4828 :                     && TREE_CODE (objsize) == INTEGER_CST
    6382         1126 :                     && tree_int_cst_le (offset, objsize)))
    6383              :               {
    6384         1539 :                 replace_call_with_value (gsi, NULL_TREE);
    6385         1539 :                 return true;
    6386              :               }
    6387              :           }
    6388              :           break;
    6389        11660 :         case IFN_UBSAN_PTR:
    6390        11660 :           if (integer_zerop (gimple_call_arg (stmt, 1)))
    6391              :             {
    6392           30 :               replace_call_with_value (gsi, NULL_TREE);
    6393           30 :               return true;
    6394              :             }
    6395              :           break;
    6396         8491 :         case IFN_UBSAN_BOUNDS:
    6397         8491 :           {
    6398         8491 :             tree index = gimple_call_arg (stmt, 1);
    6399         8491 :             tree bound = gimple_call_arg (stmt, 2);
    6400         8491 :             if (TREE_CODE (index) == INTEGER_CST
    6401         5469 :                 && TREE_CODE (bound) == INTEGER_CST)
    6402              :               {
    6403         4338 :                 index = fold_convert (TREE_TYPE (bound), index);
    6404         4338 :                 if (TREE_CODE (index) == INTEGER_CST
    6405         4338 :                     && tree_int_cst_lt (index, bound))
    6406              :                   {
    6407          286 :                     replace_call_with_value (gsi, NULL_TREE);
    6408          286 :                     return true;
    6409              :                   }
    6410              :               }
    6411              :           }
    6412              :           break;
    6413        20589 :         case IFN_GOACC_DIM_SIZE:
    6414        20589 :         case IFN_GOACC_DIM_POS:
    6415        20589 :           result = fold_internal_goacc_dim (stmt);
    6416        20589 :           break;
    6417              :         case IFN_UBSAN_CHECK_ADD:
    6418              :           subcode = PLUS_EXPR;
    6419              :           break;
    6420              :         case IFN_UBSAN_CHECK_SUB:
    6421              :           subcode = MINUS_EXPR;
    6422              :           break;
    6423              :         case IFN_UBSAN_CHECK_MUL:
    6424              :           subcode = MULT_EXPR;
    6425              :           break;
    6426              :         case IFN_ADD_OVERFLOW:
    6427              :           subcode = PLUS_EXPR;
    6428              :           cplx_result = true;
    6429              :           break;
    6430              :         case IFN_SUB_OVERFLOW:
    6431              :           subcode = MINUS_EXPR;
    6432              :           cplx_result = true;
    6433              :           break;
    6434              :         case IFN_MUL_OVERFLOW:
    6435              :           subcode = MULT_EXPR;
    6436              :           cplx_result = true;
    6437              :           break;
    6438              :         case IFN_UADDC:
    6439              :           subcode = PLUS_EXPR;
    6440              :           cplx_result = true;
    6441              :           uaddc_usubc = true;
    6442              :           break;
    6443              :         case IFN_USUBC:
    6444              :           subcode = MINUS_EXPR;
    6445              :           cplx_result = true;
    6446              :           uaddc_usubc = true;
    6447              :           break;
    6448         4357 :         case IFN_LEN_LOAD:
    6449         4357 :         case IFN_MASK_LOAD:
    6450         4357 :         case IFN_MASK_LEN_LOAD:
    6451         4357 :         case IFN_MASK_GATHER_LOAD:
    6452         4357 :         case IFN_MASK_LEN_GATHER_LOAD:
    6453         4357 :         case IFN_MASK_LOAD_LANES:
    6454         4357 :         case IFN_MASK_LEN_LOAD_LANES:
    6455         4357 :         case IFN_LEN_STORE:
    6456         4357 :         case IFN_MASK_STORE:
    6457         4357 :         case IFN_MASK_LEN_STORE:
    6458         4357 :         case IFN_MASK_SCATTER_STORE:
    6459         4357 :         case IFN_MASK_LEN_SCATTER_STORE:
    6460         4357 :         case IFN_MASK_STORE_LANES:
    6461         4357 :         case IFN_MASK_LEN_STORE_LANES:
    6462         4357 :           changed |= gimple_fold_partial_load_store (gsi, stmt);
    6463         4357 :           break;
    6464              :         default:
    6465              :           break;
    6466              :         }
    6467       198958 :       if (subcode != ERROR_MARK)
    6468              :         {
    6469       504363 :           tree arg0 = gimple_call_arg (stmt, 0);
    6470       504363 :           tree arg1 = gimple_call_arg (stmt, 1);
    6471       504363 :           tree arg2 = NULL_TREE;
    6472       504363 :           tree type = TREE_TYPE (arg0);
    6473       504363 :           if (cplx_result)
    6474              :             {
    6475       484641 :               tree lhs = gimple_call_lhs (stmt);
    6476       484641 :               if (lhs == NULL_TREE)
    6477              :                 type = NULL_TREE;
    6478              :               else
    6479       484641 :                 type = TREE_TYPE (TREE_TYPE (lhs));
    6480       484641 :               if (uaddc_usubc)
    6481        37109 :                 arg2 = gimple_call_arg (stmt, 2);
    6482              :             }
    6483       504363 :           if (type == NULL_TREE)
    6484              :             ;
    6485       504363 :           else if (uaddc_usubc)
    6486              :             {
    6487        37109 :               if (!integer_zerop (arg2))
    6488              :                 ;
    6489              :               /* x = y + 0 + 0; x = y - 0 - 0; */
    6490         5546 :               else if (integer_zerop (arg1))
    6491              :                 result = arg0;
    6492              :               /* x = 0 + y + 0; */
    6493         4912 :               else if (subcode != MINUS_EXPR && integer_zerop (arg0))
    6494              :                 result = arg1;
    6495              :               /* x = y - y - 0; */
    6496         4912 :               else if (subcode == MINUS_EXPR
    6497         4912 :                        && operand_equal_p (arg0, arg1, 0))
    6498            0 :                 result = integer_zero_node;
    6499              :             }
    6500              :           /* x = y + 0; x = y - 0; x = y * 0; */
    6501       467254 :           else if (integer_zerop (arg1))
    6502        10102 :             result = subcode == MULT_EXPR ? integer_zero_node : arg0;
    6503              :           /* x = 0 + y; x = 0 * y; */
    6504       457152 :           else if (subcode != MINUS_EXPR && integer_zerop (arg0))
    6505            0 :             result = subcode == MULT_EXPR ? integer_zero_node : arg1;
    6506              :           /* x = y - y; */
    6507       457152 :           else if (subcode == MINUS_EXPR && operand_equal_p (arg0, arg1, 0))
    6508           13 :             result = integer_zero_node;
    6509              :           /* x = y * 1; x = 1 * y; */
    6510       457139 :           else if (subcode == MULT_EXPR && integer_onep (arg1))
    6511              :             result = arg0;
    6512       451968 :           else if (subcode == MULT_EXPR && integer_onep (arg0))
    6513              :             result = arg1;
    6514       504363 :           if (result)
    6515              :             {
    6516        15920 :               if (result == integer_zero_node)
    6517         2147 :                 result = build_zero_cst (type);
    6518        13773 :               else if (cplx_result && TREE_TYPE (result) != type)
    6519              :                 {
    6520         9704 :                   if (TREE_CODE (result) == INTEGER_CST)
    6521              :                     {
    6522            0 :                       if (arith_overflowed_p (PLUS_EXPR, type, result,
    6523              :                                               integer_zero_node))
    6524            0 :                         overflow = build_one_cst (type);
    6525              :                     }
    6526         9704 :                   else if ((!TYPE_UNSIGNED (TREE_TYPE (result))
    6527         6988 :                             && TYPE_UNSIGNED (type))
    6528         9886 :                            || (TYPE_PRECISION (type)
    6529         2898 :                                < (TYPE_PRECISION (TREE_TYPE (result))
    6530         2898 :                                   + (TYPE_UNSIGNED (TREE_TYPE (result))
    6531         3289 :                                      && !TYPE_UNSIGNED (type)))))
    6532              :                     result = NULL_TREE;
    6533           60 :                   if (result)
    6534           60 :                     result = fold_convert (type, result);
    6535              :                 }
    6536              :             }
    6537              :         }
    6538              : 
    6539      1495421 :       if (result)
    6540              :         {
    6541        28914 :           if (TREE_CODE (result) == INTEGER_CST && TREE_OVERFLOW (result))
    6542            0 :             result = drop_tree_overflow (result);
    6543        28914 :           if (cplx_result)
    6544              :             {
    6545         6265 :               if (overflow == NULL_TREE)
    6546         6265 :                 overflow = build_zero_cst (TREE_TYPE (result));
    6547         6265 :               tree ctype = build_complex_type (TREE_TYPE (result));
    6548         6265 :               if (TREE_CODE (result) == INTEGER_CST
    6549         2147 :                   && TREE_CODE (overflow) == INTEGER_CST)
    6550         2147 :                 result = build_complex (ctype, result, overflow);
    6551              :               else
    6552         4118 :                 result = build2_loc (gimple_location (stmt), COMPLEX_EXPR,
    6553              :                                      ctype, result, overflow);
    6554              :             }
    6555        28914 :           gimplify_and_update_call_from_tree (gsi, result);
    6556        28914 :           changed = true;
    6557              :         }
    6558              :     }
    6559              : 
    6560              :   return changed;
    6561              : }
    6562              : 
    6563              : 
    6564              : /* Return true whether NAME has a use on STMT.  Note this can return
    6565              :    false even though there's a use on STMT if SSA operands are not
    6566              :    up-to-date.  */
    6567              : 
    6568              : static bool
    6569         1801 : has_use_on_stmt (tree name, gimple *stmt)
    6570              : {
    6571         1801 :   ssa_op_iter iter;
    6572         1801 :   tree op;
    6573         3644 :   FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
    6574         1889 :     if (op == name)
    6575              :       return true;
    6576              :   return false;
    6577              : }
    6578              : 
    6579              : /* Add the lhs of each statement of SEQ to DCE_WORKLIST. */
    6580              : 
    6581              : void
    6582      4953560 : mark_lhs_in_seq_for_dce (bitmap dce_worklist, gimple_seq seq)
    6583              : {
    6584      4953560 :   if (!dce_worklist)
    6585              :     return;
    6586              : 
    6587      1756333 :   for (gimple_stmt_iterator i = gsi_start (seq);
    6588      2038874 :        !gsi_end_p (i); gsi_next (&i))
    6589              :     {
    6590       282541 :       gimple *stmt = gsi_stmt (i);
    6591       282541 :       tree name = gimple_get_lhs (stmt);
    6592       282541 :       if (name && TREE_CODE (name) == SSA_NAME)
    6593       282541 :         bitmap_set_bit (dce_worklist, SSA_NAME_VERSION (name));
    6594              :     }
    6595              : }
    6596              : 
    6597              : /* Worker for fold_stmt_1 dispatch to pattern based folding with
    6598              :    gimple_simplify.
    6599              : 
    6600              :    Replaces *GSI with the simplification result in RCODE and OPS
    6601              :    and the associated statements in *SEQ.  Does the replacement
    6602              :    according to INPLACE and returns true if the operation succeeded.  */
    6603              : 
    6604              : static bool
    6605      9512757 : replace_stmt_with_simplification (gimple_stmt_iterator *gsi,
    6606              :                                   gimple_match_op *res_op,
    6607              :                                   gimple_seq *seq, bool inplace,
    6608              :                                   bitmap dce_worklist)
    6609              : {
    6610      9512757 :   gimple *stmt = gsi_stmt (*gsi);
    6611      9512757 :   tree *ops = res_op->ops;
    6612      9512757 :   unsigned int num_ops = res_op->num_ops;
    6613              : 
    6614              :   /* Play safe and do not allow abnormals to be mentioned in
    6615              :      newly created statements.  See also maybe_push_res_to_seq.
    6616              :      As an exception allow such uses if there was a use of the
    6617              :      same SSA name on the old stmt.  */
    6618     21077812 :   for (unsigned int i = 0; i < num_ops; ++i)
    6619     11566810 :     if (TREE_CODE (ops[i]) == SSA_NAME
    6620      6967042 :         && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops[i])
    6621     11568611 :         && !has_use_on_stmt (ops[i], stmt))
    6622              :       return false;
    6623              : 
    6624      9511002 :   if (num_ops > 0 && COMPARISON_CLASS_P (ops[0]))
    6625            0 :     for (unsigned int i = 0; i < 2; ++i)
    6626            0 :       if (TREE_CODE (TREE_OPERAND (ops[0], i)) == SSA_NAME
    6627            0 :           && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (ops[0], i))
    6628            0 :           && !has_use_on_stmt (TREE_OPERAND (ops[0], i), stmt))
    6629              :         return false;
    6630              : 
    6631              :   /* Don't insert new statements when INPLACE is true, even if we could
    6632              :      reuse STMT for the final statement.  */
    6633      9511002 :   if (inplace && !gimple_seq_empty_p (*seq))
    6634              :     return false;
    6635              : 
    6636      9511002 :   if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
    6637              :     {
    6638      7104576 :       gcc_assert (res_op->code.is_tree_code ());
    6639      7104576 :       auto code = tree_code (res_op->code);
    6640      7104576 :       if (TREE_CODE_CLASS (code) == tcc_comparison
    6641              :           /* GIMPLE_CONDs condition may not throw.  */
    6642      7104576 :           && ((cfun
    6643      1170269 :                && (!flag_exceptions
    6644       783366 :                    || !cfun->can_throw_non_call_exceptions))
    6645       568712 :               || !operation_could_trap_p (code,
    6646       568712 :                                           FLOAT_TYPE_P (TREE_TYPE (ops[0])),
    6647              :                                           false, NULL_TREE)))
    6648      1158648 :         gimple_cond_set_condition (cond_stmt, code, ops[0], ops[1]);
    6649      5945928 :       else if (code == SSA_NAME)
    6650              :         {
    6651              :           /* If setting the gimple cond to the same thing,
    6652              :              return false as nothing changed.  */
    6653      4080134 :           if (gimple_cond_code (cond_stmt) == NE_EXPR
    6654      4054962 :               && operand_equal_p (gimple_cond_lhs (cond_stmt), ops[0])
    6655      8132340 :               && integer_zerop (gimple_cond_rhs (cond_stmt)))
    6656              :             return false;
    6657        27928 :           gimple_cond_set_condition (cond_stmt, NE_EXPR, ops[0],
    6658        27928 :                                      build_zero_cst (TREE_TYPE (ops[0])));
    6659              :         }
    6660      1865794 :       else if (code == INTEGER_CST)
    6661              :         {
    6662              :           /* Make into the canonical form `1 != 0` and `0 != 0`.
    6663              :              If already in the canonical form return false
    6664              :              saying nothing has been done.  */
    6665      1317406 :           if (integer_zerop (ops[0]))
    6666              :             {
    6667       907489 :               if (gimple_cond_false_canonical_p (cond_stmt))
    6668              :                 return false;
    6669       522647 :               gimple_cond_make_false (cond_stmt);
    6670              :             }
    6671              :           else
    6672              :             {
    6673       409917 :               if (gimple_cond_true_canonical_p (cond_stmt))
    6674              :                 return false;
    6675       214940 :               gimple_cond_make_true (cond_stmt);
    6676              :             }
    6677              :         }
    6678       548388 :       else if (!inplace)
    6679              :         {
    6680              :           /* For throwing comparisons, see if the GIMPLE_COND is the same as
    6681              :              the comparison would be.
    6682              :              This can happen due to the match pattern for
    6683              :              `(ne (cmp @0 @1) integer_zerop)` which creates a new expression
    6684              :              for the comparison.  */
    6685       548388 :           if (TREE_CODE_CLASS (code) == tcc_comparison
    6686        11621 :               && (!cfun
    6687        11621 :                   || (flag_exceptions
    6688        11621 :                       && cfun->can_throw_non_call_exceptions))
    6689       560009 :               && operation_could_trap_p (code,
    6690        11621 :                                          FLOAT_TYPE_P (TREE_TYPE (ops[0])),
    6691              :                                          false, NULL_TREE))
    6692              :             {
    6693        11621 :               tree lhs = gimple_cond_lhs (cond_stmt);
    6694        11621 :               if (gimple_cond_code (cond_stmt) == NE_EXPR
    6695        11621 :                   && TREE_CODE (lhs) == SSA_NAME
    6696        11621 :                   && INTEGRAL_TYPE_P (TREE_TYPE (lhs))
    6697        23242 :                   && integer_zerop (gimple_cond_rhs (cond_stmt)))
    6698              :                 {
    6699        11621 :                   gimple *s = SSA_NAME_DEF_STMT (lhs);
    6700        11621 :                   if (is_gimple_assign (s)
    6701        11621 :                       && gimple_assign_rhs_code (s) == code
    6702        11614 :                       && operand_equal_p (gimple_assign_rhs1 (s), ops[0])
    6703        23235 :                       && operand_equal_p (gimple_assign_rhs2 (s), ops[1]))
    6704              :                     return false;
    6705              :                 }
    6706              :             }
    6707       536774 :           tree res = maybe_push_res_to_seq (res_op, seq);
    6708       536774 :           if (!res)
    6709              :             return false;
    6710       536774 :           gimple_cond_set_condition (cond_stmt, NE_EXPR, res,
    6711       536774 :                                      build_zero_cst (TREE_TYPE (res)));
    6712              :         }
    6713              :       else
    6714              :         return false;
    6715      2460937 :       if (dump_file && (dump_flags & TDF_DETAILS))
    6716              :         {
    6717          906 :           fprintf (dump_file, "gimple_simplified to ");
    6718          906 :           if (!gimple_seq_empty_p (*seq))
    6719            0 :             print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
    6720          906 :           print_gimple_stmt (dump_file, gsi_stmt (*gsi),
    6721              :                              0, TDF_SLIM);
    6722              :         }
    6723              :       // Mark the lhs of the new statements maybe for dce
    6724      2460937 :       mark_lhs_in_seq_for_dce (dce_worklist, *seq);
    6725      2460937 :       gsi_insert_seq_before (gsi, *seq, GSI_SAME_STMT);
    6726      2460937 :       return true;
    6727              :     }
    6728      2406426 :   else if (is_gimple_assign (stmt)
    6729      2406426 :            && res_op->code.is_tree_code ())
    6730              :     {
    6731      2330134 :       auto code = tree_code (res_op->code);
    6732      2330134 :       if (!inplace
    6733      2330134 :           || gimple_num_ops (stmt) > get_gimple_rhs_num_ops (code))
    6734              :         {
    6735      2330134 :           maybe_build_generic_op (res_op);
    6736      5501093 :           gimple_assign_set_rhs_with_ops (gsi, code,
    6737              :                                           res_op->op_or_null (0),
    6738              :                                           res_op->op_or_null (1),
    6739              :                                           res_op->op_or_null (2));
    6740      2330134 :           if (dump_file && (dump_flags & TDF_DETAILS))
    6741              :             {
    6742        17265 :               fprintf (dump_file, "gimple_simplified to ");
    6743        17265 :               if (!gimple_seq_empty_p (*seq))
    6744          330 :                 print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
    6745        17265 :               print_gimple_stmt (dump_file, gsi_stmt (*gsi),
    6746              :                                  0, TDF_SLIM);
    6747              :             }
    6748              :           // Mark the lhs of the new statements maybe for dce
    6749      2330134 :           mark_lhs_in_seq_for_dce (dce_worklist, *seq);
    6750      2330134 :           gsi_insert_seq_before (gsi, *seq, GSI_SAME_STMT);
    6751      2330134 :           return true;
    6752              :         }
    6753              :     }
    6754        76292 :   else if (res_op->code.is_fn_code ()
    6755        76292 :            && gimple_call_combined_fn (stmt) == combined_fn (res_op->code))
    6756              :     {
    6757         8298 :       gcc_assert (num_ops == gimple_call_num_args (stmt));
    6758        24395 :       for (unsigned int i = 0; i < num_ops; ++i)
    6759        16097 :         gimple_call_set_arg (stmt, i, ops[i]);
    6760         8298 :       if (dump_file && (dump_flags & TDF_DETAILS))
    6761              :         {
    6762            0 :           fprintf (dump_file, "gimple_simplified to ");
    6763            0 :           if (!gimple_seq_empty_p (*seq))
    6764            0 :             print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
    6765            0 :           print_gimple_stmt (dump_file, gsi_stmt (*gsi), 0, TDF_SLIM);
    6766              :         }
    6767              :       // Mark the lhs of the new statements maybe for dce
    6768         8298 :       mark_lhs_in_seq_for_dce (dce_worklist, *seq);
    6769         8298 :       gsi_insert_seq_before (gsi, *seq, GSI_SAME_STMT);
    6770         8298 :       return true;
    6771              :     }
    6772        67994 :   else if (!inplace)
    6773              :     {
    6774       135082 :       if (gimple_has_lhs (stmt))
    6775              :         {
    6776        67994 :           tree lhs = gimple_get_lhs (stmt);
    6777        67994 :           if (!maybe_push_res_to_seq (res_op, seq, lhs))
    6778              :             return false;
    6779        67005 :           if (dump_file && (dump_flags & TDF_DETAILS))
    6780              :             {
    6781           10 :               fprintf (dump_file, "gimple_simplified to ");
    6782           10 :               print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
    6783              :             }
    6784              :           // Mark the lhs of the new statements maybe for dce
    6785        67005 :           mark_lhs_in_seq_for_dce (dce_worklist, *seq);
    6786        67005 :           gsi_replace_with_seq_vops (gsi, *seq);
    6787        67005 :           return true;
    6788              :         }
    6789              :       else
    6790            0 :         gcc_unreachable ();
    6791              :     }
    6792              : 
    6793              :   return false;
    6794              : }
    6795              : 
    6796              : /* Canonicalize MEM_REFs invariant address operand after propagation.  */
    6797              : 
    6798              : static bool
    6799    200464441 : maybe_canonicalize_mem_ref_addr (tree *t, bool is_debug = false)
    6800              : {
    6801    200464441 :   bool res = false;
    6802    200464441 :   tree *orig_t = t;
    6803              : 
    6804    200464441 :   if (TREE_CODE (*t) == ADDR_EXPR)
    6805     67845991 :     t = &TREE_OPERAND (*t, 0);
    6806              : 
    6807              :   /* The C and C++ frontends use an ARRAY_REF for indexing with their
    6808              :      generic vector extension.  The actual vector referenced is
    6809              :      view-converted to an array type for this purpose.  If the index
    6810              :      is constant the canonical representation in the middle-end is a
    6811              :      BIT_FIELD_REF so re-write the former to the latter here.  */
    6812    200464441 :   if (TREE_CODE (*t) == ARRAY_REF
    6813     11764950 :       && TREE_CODE (TREE_OPERAND (*t, 0)) == VIEW_CONVERT_EXPR
    6814       194479 :       && TREE_CODE (TREE_OPERAND (*t, 1)) == INTEGER_CST
    6815    200521999 :       && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (*t, 0), 0))))
    6816              :     {
    6817        26714 :       tree vtype = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (*t, 0), 0));
    6818        26714 :       if (VECTOR_TYPE_P (vtype))
    6819              :         {
    6820        26714 :           tree low = array_ref_low_bound (*t);
    6821        26714 :           if (TREE_CODE (low) == INTEGER_CST)
    6822              :             {
    6823        26714 :               if (tree_int_cst_le (low, TREE_OPERAND (*t, 1)))
    6824              :                 {
    6825        53368 :                   widest_int idx = wi::sub (wi::to_widest (TREE_OPERAND (*t, 1)),
    6826        53368 :                                             wi::to_widest (low));
    6827        26684 :                   idx = wi::mul (idx, wi::to_widest
    6828        53368 :                                          (TYPE_SIZE (TREE_TYPE (*t))));
    6829        26684 :                   widest_int ext
    6830        26684 :                     = wi::add (idx, wi::to_widest (TYPE_SIZE (TREE_TYPE (*t))));
    6831        26684 :                   if (maybe_le (ext, wi::to_poly_widest (TYPE_SIZE (vtype))))
    6832              :                     {
    6833        52216 :                       *t = build3_loc (EXPR_LOCATION (*t), BIT_FIELD_REF,
    6834        26108 :                                        TREE_TYPE (*t),
    6835        26108 :                                        TREE_OPERAND (TREE_OPERAND (*t, 0), 0),
    6836        26108 :                                        TYPE_SIZE (TREE_TYPE (*t)),
    6837        26108 :                                        wide_int_to_tree (bitsizetype, idx));
    6838        26108 :                       res = true;
    6839              :                     }
    6840        26684 :                 }
    6841              :             }
    6842              :         }
    6843              :     }
    6844              : 
    6845    378602478 :   while (handled_component_p (*t))
    6846    178138037 :     t = &TREE_OPERAND (*t, 0);
    6847              : 
    6848              :   /* Canonicalize MEM [&foo.bar, 0] which appears after propagating
    6849              :      of invariant addresses into a SSA name MEM_REF address.  */
    6850    200464441 :   if (TREE_CODE (*t) == MEM_REF
    6851    200464441 :       || TREE_CODE (*t) == TARGET_MEM_REF)
    6852              :     {
    6853    105368152 :       tree addr = TREE_OPERAND (*t, 0);
    6854    105368152 :       if (TREE_CODE (addr) == ADDR_EXPR
    6855    105368152 :           && (TREE_CODE (TREE_OPERAND (addr, 0)) == MEM_REF
    6856     31932521 :               || handled_component_p (TREE_OPERAND (addr, 0))))
    6857              :         {
    6858       543569 :           tree base;
    6859       543569 :           poly_int64 coffset;
    6860       543569 :           base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0),
    6861              :                                                 &coffset);
    6862       543569 :           if (!base)
    6863              :             {
    6864           18 :               if (is_debug)
    6865           18 :                 return false;
    6866            0 :               gcc_unreachable ();
    6867              :             }
    6868              : 
    6869       543551 :           TREE_OPERAND (*t, 0) = build_fold_addr_expr (base);
    6870       543551 :           TREE_OPERAND (*t, 1) = int_const_binop (PLUS_EXPR,
    6871       543551 :                                                   TREE_OPERAND (*t, 1),
    6872       543551 :                                                   size_int (coffset));
    6873       543551 :           res = true;
    6874              :         }
    6875    105368134 :       gcc_checking_assert (TREE_CODE (TREE_OPERAND (*t, 0)) == DEBUG_EXPR_DECL
    6876              :                            || is_gimple_mem_ref_addr (TREE_OPERAND (*t, 0)));
    6877              :     }
    6878              : 
    6879              :   /* Canonicalize back MEM_REFs to plain reference trees if the object
    6880              :      accessed is a decl that has the same access semantics as the MEM_REF.  */
    6881    200464423 :   if (TREE_CODE (*t) == MEM_REF
    6882    103369623 :       && TREE_CODE (TREE_OPERAND (*t, 0)) == ADDR_EXPR
    6883     31635672 :       && integer_zerop (TREE_OPERAND (*t, 1))
    6884    217794373 :       && MR_DEPENDENCE_CLIQUE (*t) == 0)
    6885              :     {
    6886     12241088 :       tree decl = TREE_OPERAND (TREE_OPERAND (*t, 0), 0);
    6887     12241088 :       tree alias_type = TREE_TYPE (TREE_OPERAND (*t, 1));
    6888     12241088 :       if (/* Same volatile qualification.  */
    6889     12241088 :           TREE_THIS_VOLATILE (*t) == TREE_THIS_VOLATILE (decl)
    6890              :           /* Same TBAA behavior with -fstrict-aliasing.  */
    6891     12238084 :           && !TYPE_REF_CAN_ALIAS_ALL (alias_type)
    6892     11876121 :           && (TYPE_MAIN_VARIANT (TREE_TYPE (decl))
    6893     11876121 :               == TYPE_MAIN_VARIANT (TREE_TYPE (alias_type)))
    6894              :           /* Same alignment.  */
    6895      4890957 :           && TYPE_ALIGN (TREE_TYPE (decl)) == TYPE_ALIGN (TREE_TYPE (*t))
    6896              :           /* We have to look out here to not drop a required conversion
    6897              :              from the rhs to the lhs if *t appears on the lhs or vice-versa
    6898              :              if it appears on the rhs.  Thus require strict type
    6899              :              compatibility.  */
    6900     16845703 :           && types_compatible_p (TREE_TYPE (*t), TREE_TYPE (decl)))
    6901              :         {
    6902      2526866 :           *t = TREE_OPERAND (TREE_OPERAND (*t, 0), 0);
    6903      2526866 :           res = true;
    6904              :         }
    6905              :     }
    6906              : 
    6907    188223335 :   else if (TREE_CODE (*orig_t) == ADDR_EXPR
    6908     65011040 :            && TREE_CODE (*t) == MEM_REF
    6909    210788389 :            && TREE_CODE (TREE_OPERAND (*t, 0)) == INTEGER_CST)
    6910              :     {
    6911          898 :       tree base;
    6912          898 :       poly_int64 coffset;
    6913          898 :       base = get_addr_base_and_unit_offset (TREE_OPERAND (*orig_t, 0),
    6914              :                                             &coffset);
    6915          898 :       if (base)
    6916              :         {
    6917          770 :           gcc_assert (TREE_CODE (base) == MEM_REF);
    6918          770 :           poly_int64 moffset;
    6919          770 :           if (mem_ref_offset (base).to_shwi (&moffset))
    6920              :             {
    6921          770 :               coffset += moffset;
    6922          770 :               if (wi::to_poly_wide (TREE_OPERAND (base, 0)).to_shwi (&moffset))
    6923              :                 {
    6924          770 :                   coffset += moffset;
    6925          770 :                   *orig_t = build_int_cst (TREE_TYPE (*orig_t), coffset);
    6926          770 :                   return true;
    6927              :                 }
    6928              :             }
    6929              :         }
    6930              :     }
    6931              : 
    6932              :   /* Canonicalize TARGET_MEM_REF in particular with respect to
    6933              :      the indexes becoming constant.  */
    6934    188222437 :   else if (TREE_CODE (*t) == TARGET_MEM_REF)
    6935              :     {
    6936      1998511 :       tree tem = maybe_fold_tmr (*t);
    6937      1998511 :       if (tem)
    6938              :         {
    6939         4178 :           *t = tem;
    6940         4178 :           if (TREE_CODE (*orig_t) == ADDR_EXPR)
    6941            0 :             recompute_tree_invariant_for_addr_expr (*orig_t);
    6942              :           res = true;
    6943              :         }
    6944              :     }
    6945              : 
    6946              :   return res;
    6947              : }
    6948              : 
    6949              : /* Worker for both fold_stmt and fold_stmt_inplace.  The INPLACE argument
    6950              :    distinguishes both cases.  */
    6951              : 
    6952              : static bool
    6953    809373928 : fold_stmt_1 (gimple_stmt_iterator *gsi, bool inplace, tree (*valueize) (tree),
    6954              :              bitmap dce_worklist = nullptr)
    6955              : {
    6956    809373928 :   bool changed = false;
    6957    809373928 :   gimple *stmt = gsi_stmt (*gsi);
    6958    809373928 :   unsigned i;
    6959              : 
    6960              :   /* First do required canonicalization of [TARGET_]MEM_REF addresses
    6961              :      after propagation.
    6962              :      ???  This shouldn't be done in generic folding but in the
    6963              :      propagation helpers which also know whether an address was
    6964              :      propagated.
    6965              :      Also canonicalize operand order.  */
    6966    809373928 :   switch (gimple_code (stmt))
    6967              :     {
    6968    266036217 :     case GIMPLE_ASSIGN:
    6969    266036217 :       if (gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS)
    6970              :         {
    6971    175379428 :           tree *rhs = gimple_assign_rhs1_ptr (stmt);
    6972    175379428 :           if ((REFERENCE_CLASS_P (*rhs)
    6973    111165510 :                || TREE_CODE (*rhs) == ADDR_EXPR)
    6974    190798836 :               && maybe_canonicalize_mem_ref_addr (rhs))
    6975              :             changed = true;
    6976    175379428 :           tree *lhs = gimple_assign_lhs_ptr (stmt);
    6977    175379428 :           if (REFERENCE_CLASS_P (*lhs)
    6978    175379428 :               && maybe_canonicalize_mem_ref_addr (lhs))
    6979              :             changed = true;
    6980              :           /* Canonicalize &MEM[ssa_n, CST] to ssa_n p+ CST.
    6981              :              This cannot be done in maybe_canonicalize_mem_ref_addr
    6982              :              as the gimple now has two operands rather than one.
    6983              :              The same reason why this can't be done in
    6984              :              maybe_canonicalize_mem_ref_addr is the same reason why
    6985              :              this can't be done inplace.  */
    6986    175379428 :           if (!inplace && TREE_CODE (*rhs) == ADDR_EXPR)
    6987              :             {
    6988     15217431 :               tree inner = TREE_OPERAND (*rhs, 0);
    6989     15217431 :               if (TREE_CODE (inner) == MEM_REF
    6990      1084178 :                   && TREE_CODE (TREE_OPERAND (inner, 0)) == SSA_NAME
    6991     15283990 :                   && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
    6992              :                 {
    6993        66559 :                   tree ptr = TREE_OPERAND (inner, 0);
    6994        66559 :                   tree addon = TREE_OPERAND (inner, 1);
    6995        66559 :                   addon = fold_convert (sizetype, addon);
    6996        66559 :                   gimple_assign_set_rhs_with_ops (gsi, POINTER_PLUS_EXPR,
    6997              :                                                   ptr, addon);
    6998        66559 :                   changed = true;
    6999        66559 :                   stmt = gsi_stmt (*gsi);
    7000              :                 }
    7001              :             }
    7002              :         }
    7003              :       else
    7004              :         {
    7005              :           /* Canonicalize operand order.  */
    7006     90656789 :           enum tree_code code = gimple_assign_rhs_code (stmt);
    7007     90656789 :           if (TREE_CODE_CLASS (code) == tcc_comparison
    7008     84325403 :               || commutative_tree_code (code)
    7009    136249977 :               || commutative_ternary_tree_code (code))
    7010              :             {
    7011     45064783 :               tree rhs1 = gimple_assign_rhs1 (stmt);
    7012     45064783 :               tree rhs2 = gimple_assign_rhs2 (stmt);
    7013     45064783 :               if (tree_swap_operands_p (rhs1, rhs2))
    7014              :                 {
    7015      2688849 :                   gimple_assign_set_rhs1 (stmt, rhs2);
    7016      2688849 :                   gimple_assign_set_rhs2 (stmt, rhs1);
    7017      2688849 :                   if (TREE_CODE_CLASS (code) == tcc_comparison)
    7018       324648 :                     gimple_assign_set_rhs_code (stmt,
    7019              :                                                 swap_tree_comparison (code));
    7020              :                   changed = true;
    7021              :                 }
    7022              :             }
    7023              :         }
    7024              :       break;
    7025     58411894 :     case GIMPLE_CALL:
    7026     58411894 :       {
    7027     58411894 :         gcall *call = as_a<gcall *> (stmt);
    7028    233894652 :         for (i = 0; i < gimple_call_num_args (call); ++i)
    7029              :           {
    7030    117070864 :             tree *arg = gimple_call_arg_ptr (call, i);
    7031    117070864 :             if (REFERENCE_CLASS_P (*arg)
    7032    117070864 :                 && maybe_canonicalize_mem_ref_addr (arg))
    7033              :               changed = true;
    7034              :           }
    7035     58411894 :         tree *lhs = gimple_call_lhs_ptr (call);
    7036     58411894 :         if (*lhs
    7037     23699034 :             && REFERENCE_CLASS_P (*lhs)
    7038     58537613 :             && maybe_canonicalize_mem_ref_addr (lhs))
    7039              :           changed = true;
    7040     58411894 :         if (*lhs)
    7041              :           {
    7042     23699034 :             combined_fn cfn = gimple_call_combined_fn (call);
    7043     23699034 :             internal_fn ifn = associated_internal_fn (cfn, TREE_TYPE (*lhs));
    7044     23699034 :             int opno = first_commutative_argument (ifn);
    7045     23699034 :             if (opno >= 0)
    7046              :               {
    7047       367513 :                 tree arg1 = gimple_call_arg (call, opno);
    7048       367513 :                 tree arg2 = gimple_call_arg (call, opno + 1);
    7049       367513 :                 if (tree_swap_operands_p (arg1, arg2))
    7050              :                   {
    7051        22979 :                     gimple_call_set_arg (call, opno, arg2);
    7052        22979 :                     gimple_call_set_arg (call, opno + 1, arg1);
    7053        22979 :                     changed = true;
    7054              :                   }
    7055              :               }
    7056              :           }
    7057              :         break;
    7058              :       }
    7059       550498 :     case GIMPLE_ASM:
    7060       550498 :       {
    7061       550498 :         gasm *asm_stmt = as_a <gasm *> (stmt);
    7062      1787248 :         for (i = 0; i < gimple_asm_noutputs (asm_stmt); ++i)
    7063              :           {
    7064       686252 :             tree link = gimple_asm_output_op (asm_stmt, i);
    7065       686252 :             tree op = TREE_VALUE (link);
    7066       686252 :             if (REFERENCE_CLASS_P (op)
    7067       686252 :                 && maybe_canonicalize_mem_ref_addr (&TREE_VALUE (link)))
    7068              :               changed = true;
    7069              :           }
    7070       980183 :         for (i = 0; i < gimple_asm_ninputs (asm_stmt); ++i)
    7071              :           {
    7072       429685 :             tree link = gimple_asm_input_op (asm_stmt, i);
    7073       429685 :             tree op = TREE_VALUE (link);
    7074       429685 :             if ((REFERENCE_CLASS_P (op)
    7075       407274 :                  || TREE_CODE (op) == ADDR_EXPR)
    7076       461625 :                 && maybe_canonicalize_mem_ref_addr (&TREE_VALUE (link)))
    7077              :               changed = true;
    7078              :           }
    7079              :       }
    7080              :       break;
    7081    416928098 :     case GIMPLE_DEBUG:
    7082    416928098 :       if (gimple_debug_bind_p (stmt))
    7083              :         {
    7084    322606966 :           tree *val = gimple_debug_bind_get_value_ptr (stmt);
    7085    322606966 :           if (*val
    7086    184611130 :               && (REFERENCE_CLASS_P (*val)
    7087    181972239 :                   || TREE_CODE (*val) == ADDR_EXPR)
    7088    377640500 :               && maybe_canonicalize_mem_ref_addr (val, true))
    7089              :             changed = true;
    7090              :         }
    7091              :       break;
    7092     46315421 :     case GIMPLE_COND:
    7093     46315421 :       {
    7094              :         /* Canonicalize operand order.  */
    7095     46315421 :         tree lhs = gimple_cond_lhs (stmt);
    7096     46315421 :         tree rhs = gimple_cond_rhs (stmt);
    7097     46315421 :         if (tree_swap_operands_p (lhs, rhs))
    7098              :           {
    7099      1464924 :             gcond *gc = as_a <gcond *> (stmt);
    7100      1464924 :             gimple_cond_set_lhs (gc, rhs);
    7101      1464924 :             gimple_cond_set_rhs (gc, lhs);
    7102      1464924 :             gimple_cond_set_code (gc,
    7103              :                                   swap_tree_comparison (gimple_cond_code (gc)));
    7104      1464924 :             changed = true;
    7105              :           }
    7106              :       }
    7107    807835881 :     default:;
    7108              :     }
    7109              : 
    7110              :   /* Dispatch to pattern-based folding.  */
    7111    807835881 :   if (!inplace
    7112      3245116 :       || is_gimple_assign (stmt)
    7113    808747705 :       || gimple_code (stmt) == GIMPLE_COND)
    7114              :     {
    7115    808462104 :       gimple_seq seq = NULL;
    7116    808462104 :       gimple_match_op res_op;
    7117   1614590916 :       if (gimple_simplify (stmt, &res_op, inplace ? NULL : &seq,
    7118              :                            valueize, valueize)
    7119    808462104 :           && replace_stmt_with_simplification (gsi, &res_op, &seq, inplace,
    7120              :                                                dce_worklist))
    7121              :         changed = true;
    7122              :       else
    7123    803595730 :         gimple_seq_discard (seq);
    7124              :     }
    7125              : 
    7126    809373928 :   stmt = gsi_stmt (*gsi);
    7127              : 
    7128              :   /* Fold the main computation performed by the statement.  */
    7129    809373928 :   switch (gimple_code (stmt))
    7130              :     {
    7131    266092331 :     case GIMPLE_ASSIGN:
    7132    266092331 :       {
    7133              :         /* Try to canonicalize for boolean-typed X the comparisons
    7134              :            X == 0, X == 1, X != 0, and X != 1.  */
    7135    266092331 :         if (gimple_assign_rhs_code (stmt) == EQ_EXPR
    7136    266092331 :             || gimple_assign_rhs_code (stmt) == NE_EXPR)
    7137              :           {
    7138      3553670 :             tree lhs = gimple_assign_lhs (stmt);
    7139      3553670 :             tree op1 = gimple_assign_rhs1 (stmt);
    7140      3553670 :             tree op2 = gimple_assign_rhs2 (stmt);
    7141      3553670 :             tree type = TREE_TYPE (op1);
    7142              : 
    7143              :             /* Check whether the comparison operands are of the same boolean
    7144              :                type as the result type is.
    7145              :                Check that second operand is an integer-constant with value
    7146              :                one or zero.  */
    7147      3553670 :             if (TREE_CODE (op2) == INTEGER_CST
    7148      2445739 :                 && (integer_zerop (op2) || integer_onep (op2))
    7149      5357605 :                 && useless_type_conversion_p (TREE_TYPE (lhs), type))
    7150              :               {
    7151         4727 :                 enum tree_code cmp_code = gimple_assign_rhs_code (stmt);
    7152         4727 :                 bool is_logical_not = false;
    7153              : 
    7154              :                 /* X == 0 and X != 1 is a logical-not.of X
    7155              :                    X == 1 and X != 0 is X  */
    7156         3889 :                 if ((cmp_code == EQ_EXPR && integer_zerop (op2))
    7157         4727 :                     || (cmp_code == NE_EXPR && integer_onep (op2)))
    7158         4670 :                   is_logical_not = true;
    7159              : 
    7160         4727 :                 if (is_logical_not == false)
    7161           57 :                   gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op1), op1);
    7162              :                 /* Only for one-bit precision typed X the transformation
    7163              :                    !X -> ~X is valid.  */
    7164         4670 :                 else if (TYPE_PRECISION (type) == 1)
    7165         4670 :                   gimple_assign_set_rhs_with_ops (gsi, BIT_NOT_EXPR, op1);
    7166              :                 /* Otherwise we use !X -> X ^ 1.  */
    7167              :                 else
    7168            0 :                   gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op1,
    7169              :                                                   build_int_cst (type, 1));
    7170              :                 changed = true;
    7171              :                 break;
    7172              :               }
    7173              :           }
    7174              : 
    7175    266087604 :         unsigned old_num_ops = gimple_num_ops (stmt);
    7176    266087604 :         tree lhs = gimple_assign_lhs (stmt);
    7177    266087604 :         tree new_rhs = fold_gimple_assign (gsi);
    7178    266087604 :         if (new_rhs
    7179    266212089 :             && !useless_type_conversion_p (TREE_TYPE (lhs),
    7180       124485 :                                            TREE_TYPE (new_rhs)))
    7181            0 :           new_rhs = fold_convert (TREE_TYPE (lhs), new_rhs);
    7182    266087604 :         if (new_rhs
    7183    266087604 :             && (!inplace
    7184         1091 :                 || get_gimple_rhs_num_ops (TREE_CODE (new_rhs)) < old_num_ops))
    7185              :           {
    7186       124485 :             gimple_assign_set_rhs_from_tree (gsi, new_rhs);
    7187       124485 :             changed = true;
    7188              :           }
    7189              :         break;
    7190              :       }
    7191              : 
    7192     58355780 :     case GIMPLE_CALL:
    7193     58355780 :       changed |= gimple_fold_call (gsi, inplace);
    7194     58355780 :       break;
    7195              : 
    7196    416928098 :     case GIMPLE_DEBUG:
    7197    416928098 :       if (gimple_debug_bind_p (stmt))
    7198              :         {
    7199    322606966 :           tree val = gimple_debug_bind_get_value (stmt);
    7200    322606966 :           if (val && REFERENCE_CLASS_P (val))
    7201              :             {
    7202      2636914 :               tree tem = maybe_fold_reference (val);
    7203      2636914 :               if (tem)
    7204              :                 {
    7205         4440 :                   gimple_debug_bind_set_value (stmt, tem);
    7206         4440 :                   changed = true;
    7207              :                 }
    7208              :             }
    7209              :         }
    7210              :       break;
    7211              : 
    7212     11043852 :     case GIMPLE_RETURN:
    7213     11043852 :       {
    7214     11043852 :         greturn *ret_stmt = as_a<greturn *> (stmt);
    7215     11043852 :         tree ret = gimple_return_retval(ret_stmt);
    7216              : 
    7217     11043852 :         if (ret && TREE_CODE (ret) == SSA_NAME && valueize)
    7218              :           {
    7219      4618859 :             tree val = valueize (ret);
    7220      4618859 :             if (val && val != ret
    7221      4618859 :                 && may_propagate_copy (ret, val))
    7222              :               {
    7223            0 :                 gimple_return_set_retval (ret_stmt, val);
    7224            0 :                 changed = true;
    7225              :               }
    7226              :           }
    7227              :       }
    7228              :       break;
    7229              : 
    7230    809373928 :     default:;
    7231              :     }
    7232              : 
    7233    809373928 :   return changed;
    7234              : }
    7235              : 
    7236              : /* Valueziation callback that ends up not following SSA edges.  */
    7237              : 
    7238              : tree
    7239   6726620769 : no_follow_ssa_edges (tree)
    7240              : {
    7241   6726620769 :   return NULL_TREE;
    7242              : }
    7243              : 
    7244              : /* Valueization callback that ends up following single-use SSA edges only.  */
    7245              : 
    7246              : tree
    7247   1001247284 : follow_single_use_edges (tree val)
    7248              : {
    7249   1001247284 :   if (TREE_CODE (val) == SSA_NAME
    7250   1001247284 :       && !has_single_use (val))
    7251    516366561 :     return NULL_TREE;
    7252              :   return val;
    7253              : }
    7254              : 
    7255              : /* Valueization callback that follows all SSA edges.  */
    7256              : 
    7257              : tree
    7258    202818930 : follow_all_ssa_edges (tree val)
    7259              : {
    7260    202818930 :   return val;
    7261              : }
    7262              : 
    7263              : /* Fold the statement pointed to by GSI.  In some cases, this function may
    7264              :    replace the whole statement with a new one.  Returns true iff folding
    7265              :    makes any changes.
    7266              :    The statement pointed to by GSI should be in valid gimple form but may
    7267              :    be in unfolded state as resulting from for example constant propagation
    7268              :    which can produce *&x = 0.  */
    7269              : 
    7270              : bool
    7271    162195802 : fold_stmt (gimple_stmt_iterator *gsi, bitmap dce_bitmap)
    7272              : {
    7273    162195802 :   return fold_stmt_1 (gsi, false, no_follow_ssa_edges, dce_bitmap);
    7274              : }
    7275              : 
    7276              : bool
    7277    643933010 : fold_stmt (gimple_stmt_iterator *gsi, tree (*valueize) (tree), bitmap dce_bitmap)
    7278              : {
    7279    643933010 :   return fold_stmt_1 (gsi, false, valueize, dce_bitmap);
    7280              : }
    7281              : 
    7282              : /* Perform the minimal folding on statement *GSI.  Only operations like
    7283              :    *&x created by constant propagation are handled.  The statement cannot
    7284              :    be replaced with a new one.  Return true if the statement was
    7285              :    changed, false otherwise.
    7286              :    The statement *GSI should be in valid gimple form but may
    7287              :    be in unfolded state as resulting from for example constant propagation
    7288              :    which can produce *&x = 0.  */
    7289              : 
    7290              : bool
    7291      3245116 : fold_stmt_inplace (gimple_stmt_iterator *gsi, tree (*valueize) (tree))
    7292              : {
    7293      3245116 :   gimple *stmt = gsi_stmt (*gsi);
    7294      3245116 :   bool changed = fold_stmt_1 (gsi, true, valueize);
    7295      3245116 :   gcc_assert (gsi_stmt (*gsi) == stmt);
    7296      3245116 :   return changed;
    7297              : }
    7298              : 
    7299              : /* Canonicalize and possibly invert the boolean EXPR; return NULL_TREE
    7300              :    if EXPR is null or we don't know how.
    7301              :    If non-null, the result always has boolean type.  */
    7302              : 
    7303              : static tree
    7304       213762 : canonicalize_bool (tree expr, bool invert)
    7305              : {
    7306       213762 :   if (!expr)
    7307              :     return NULL_TREE;
    7308           25 :   else if (invert)
    7309              :     {
    7310           17 :       if (integer_nonzerop (expr))
    7311            0 :         return boolean_false_node;
    7312           17 :       else if (integer_zerop (expr))
    7313            0 :         return boolean_true_node;
    7314           17 :       else if (TREE_CODE (expr) == SSA_NAME)
    7315            0 :         return fold_build2 (EQ_EXPR, boolean_type_node, expr,
    7316              :                             build_int_cst (TREE_TYPE (expr), 0));
    7317           17 :       else if (COMPARISON_CLASS_P (expr))
    7318              :         {
    7319           17 :           tree_code code
    7320           17 :             = invert_tree_comparison (TREE_CODE (expr),
    7321           17 :                                       HONOR_NANS (TREE_OPERAND (expr, 0)));
    7322           17 :           if (code == ERROR_MARK)
    7323              :             return NULL_TREE;
    7324           16 :           return fold_build2 (code, boolean_type_node,
    7325              :                               TREE_OPERAND (expr, 0),
    7326              :                               TREE_OPERAND (expr, 1));
    7327              :         }
    7328              :       else
    7329              :         return NULL_TREE;
    7330              :     }
    7331              :   else
    7332              :     {
    7333            8 :       if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE)
    7334              :         return expr;
    7335            0 :       if (integer_nonzerop (expr))
    7336            0 :         return boolean_true_node;
    7337            0 :       else if (integer_zerop (expr))
    7338            0 :         return boolean_false_node;
    7339            0 :       else if (TREE_CODE (expr) == SSA_NAME)
    7340            0 :         return fold_build2 (NE_EXPR, boolean_type_node, expr,
    7341              :                             build_int_cst (TREE_TYPE (expr), 0));
    7342            0 :       else if (COMPARISON_CLASS_P (expr))
    7343            0 :         return fold_build2 (TREE_CODE (expr),
    7344              :                             boolean_type_node,
    7345              :                             TREE_OPERAND (expr, 0),
    7346              :                             TREE_OPERAND (expr, 1));
    7347              :       else
    7348              :         return NULL_TREE;
    7349              :     }
    7350              : }
    7351              : 
    7352              : /* Check to see if a boolean expression EXPR is logically equivalent to the
    7353              :    comparison (OP1 CODE OP2).  Check for various identities involving
    7354              :    SSA_NAMEs.  */
    7355              : 
    7356              : static bool
    7357          936 : same_bool_comparison_p (const_tree expr, enum tree_code code,
    7358              :                         const_tree op1, const_tree op2)
    7359              : {
    7360              : 
    7361              :   /* The obvious case.  */
    7362          936 :   if (TREE_CODE (expr) == code
    7363            7 :       && operand_equal_p (TREE_OPERAND (expr, 0), op1, 0)
    7364          943 :       && operand_equal_p (TREE_OPERAND (expr, 1), op2, 0))
    7365              :     return true;
    7366              : 
    7367              :   /* Check for comparing (name, name != 0) and the case where expr
    7368              :      is an SSA_NAME with a definition matching the comparison.  */
    7369          929 :   if (TREE_CODE (expr) == SSA_NAME
    7370          929 :       && TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE)
    7371              :     {
    7372            0 :       if (operand_equal_p (expr, op1, 0))
    7373            0 :         return ((code == NE_EXPR && integer_zerop (op2))
    7374            0 :                 || (code == EQ_EXPR && integer_nonzerop (op2)));
    7375            0 :       auto s = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (expr));
    7376            0 :       if (s
    7377            0 :           && gimple_assign_rhs_code (s) == code
    7378            0 :           && operand_equal_p (gimple_assign_rhs1 (s), op1, 0)
    7379            0 :           && operand_equal_p (gimple_assign_rhs2 (s), op2, 0))
    7380              :         return true;
    7381              :     }
    7382              : 
    7383              :   /* If op1 is of the form (name != 0) or (name == 0), and the definition
    7384              :      of name is a comparison, recurse.  */
    7385          929 :   if (TREE_CODE (op1) == SSA_NAME
    7386          929 :       && TREE_CODE (TREE_TYPE (op1)) == BOOLEAN_TYPE)
    7387              :     {
    7388          134 :       auto s = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (op1));
    7389           31 :       if (s
    7390           31 :           && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison)
    7391              :         {
    7392            1 :           enum tree_code c = gimple_assign_rhs_code (s);
    7393            1 :           tree rhs1 = gimple_assign_rhs1 (s);
    7394            1 :           if ((code == EQ_EXPR || code == NE_EXPR)
    7395            0 :               && TREE_CODE (op2) == INTEGER_CST)
    7396              :             {
    7397              :               /* OP1 == 0 and OP1 != true test the inverse.  */
    7398            0 :               if ((code == EQ_EXPR) == integer_zerop (op2))
    7399            0 :                 c = invert_tree_comparison (c, HONOR_NANS (rhs1));
    7400            0 :               return (c != ERROR_MARK
    7401            0 :                       && same_bool_comparison_p (expr, c, rhs1,
    7402            0 :                                                  gimple_assign_rhs2 (s)));
    7403              :             }
    7404              :         }
    7405              :     }
    7406              :   return false;
    7407              : }
    7408              : 
    7409              : /* Check to see if two boolean expressions OP1 and OP2 are logically
    7410              :    equivalent.  */
    7411              : 
    7412              : static bool
    7413            1 : same_bool_result_p (const_tree op1, const_tree op2)
    7414              : {
    7415              :   /* Simple cases first.  */
    7416            1 :   if (operand_equal_p (op1, op2, 0))
    7417              :     return true;
    7418              : 
    7419              :   /* Check the cases where at least one of the operands is a comparison.
    7420              :      These are a bit smarter than operand_equal_p in that they apply some
    7421              :      identifies on SSA_NAMEs.  */
    7422            1 :   if (COMPARISON_CLASS_P (op2)
    7423            2 :       && same_bool_comparison_p (op1, TREE_CODE (op2),
    7424            1 :                                  TREE_OPERAND (op2, 0),
    7425            1 :                                  TREE_OPERAND (op2, 1)))
    7426              :     return true;
    7427            1 :   if (COMPARISON_CLASS_P (op1)
    7428            2 :       && same_bool_comparison_p (op2, TREE_CODE (op1),
    7429            1 :                                  TREE_OPERAND (op1, 0),
    7430            1 :                                  TREE_OPERAND (op1, 1)))
    7431              :     return true;
    7432              : 
    7433              :   /* Default case.  */
    7434              :   return false;
    7435              : }
    7436              : 
    7437              : /* Forward declarations for some mutually recursive functions.  */
    7438              : 
    7439              : static tree
    7440              : and_comparisons_1 (tree type, enum tree_code code,
    7441              :                    enum tree_code code1, tree op1a, tree op1b,
    7442              :                    enum tree_code code2, tree op2a, tree op2b, basic_block);
    7443              : static tree
    7444              : and_var_with_comparison (tree type, enum tree_code code, tree var,
    7445              :                          bool invert, enum tree_code code2, tree op2a,
    7446              :                          tree op2b, basic_block);
    7447              : static tree
    7448              : and_var_with_comparison_1 (tree type, enum tree_code code, gassign *stmt,
    7449              :                            enum tree_code code2, tree op2a, tree op2b,
    7450              :                            basic_block);
    7451              : static tree
    7452              : or_comparisons_1 (tree, enum tree_code code,
    7453              :                   enum tree_code code1, tree op1a, tree op1b,
    7454              :                   enum tree_code code2, tree op2a, tree op2b,
    7455              :                   basic_block);
    7456              : static tree
    7457              : or_var_with_comparison (tree, enum tree_code code, tree var,
    7458              :                         bool invert, enum tree_code code2, tree op2a,
    7459              :                         tree op2b, basic_block);
    7460              : static tree
    7461              : or_var_with_comparison_1 (tree, enum tree_code code, gassign *stmt,
    7462              :                           enum tree_code code2, tree op2a, tree op2b,
    7463              :                           basic_block);
    7464              : 
    7465              : /* Return the OR code that evaluates its operands as the AND code CODE
    7466              :    does, or the AND code for the OR code CODE.  */
    7467              : 
    7468              : static enum tree_code
    7469        86989 : truth_andor_compl (enum tree_code code)
    7470              : {
    7471        86989 :   switch (code)
    7472              :     {
    7473              :     case TRUTH_AND_EXPR: return TRUTH_OR_EXPR;
    7474              :     case TRUTH_ANDIF_EXPR: return TRUTH_ORIF_EXPR;
    7475              :     case TRUTH_OR_EXPR: return TRUTH_AND_EXPR;
    7476              :     case TRUTH_ORIF_EXPR: return TRUTH_ANDIF_EXPR;
    7477            0 :     default: gcc_unreachable ();
    7478              :     }
    7479              : }
    7480              : 
    7481              : /* Helper function for and_comparisons_1:  try to simplify the AND of the
    7482              :    ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
    7483              :    If INVERT is true, invert the value of the VAR before doing the AND.
    7484              :    Return NULL_EXPR if we can't simplify this to a single expression.  */
    7485              : 
    7486              : static tree
    7487       183028 : and_var_with_comparison (tree type, enum tree_code code, tree var,
    7488              :                          bool invert, enum tree_code code2, tree op2a,
    7489              :                          tree op2b, basic_block outer_cond_bb)
    7490              : {
    7491       183028 :   tree t;
    7492       183028 :   gassign *stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (var));
    7493              : 
    7494              :   /* We can only deal with variables whose definitions are assignments.  */
    7495       182684 :   if (!stmt)
    7496              :     return NULL_TREE;
    7497              : 
    7498              :   /* If we have an inverted comparison, apply DeMorgan's law and rewrite
    7499              :      !var AND (op2a code2 op2b) => !(var OR !(op2a code2 op2b))
    7500              :      Then we only have to consider the simpler non-inverted cases.  */
    7501       182684 :   if (invert)
    7502              :     {
    7503        65045 :       code2 = invert_tree_comparison (code2, HONOR_NANS (op2a));
    7504        65045 :       if (code2 == ERROR_MARK)
    7505              :         return NULL_TREE;
    7506        65045 :       t = or_var_with_comparison_1 (type, truth_andor_compl (code), stmt,
    7507              :                                     code2, op2a, op2b, outer_cond_bb);
    7508              :     }
    7509              :   else
    7510       117639 :     t = and_var_with_comparison_1 (type, code, stmt, code2, op2a, op2b,
    7511              :                                    outer_cond_bb);
    7512       182684 :   return canonicalize_bool (t, invert);
    7513              : }
    7514              : 
    7515              : /* Try to simplify the AND of the ssa variable defined by the assignment
    7516              :    STMT with the comparison specified by (OP2A CODE2 OP2B).
    7517              :    Return NULL_EXPR if we can't simplify this to a single expression.  */
    7518              : 
    7519              : static tree
    7520       139583 : and_var_with_comparison_1 (tree type, enum tree_code code, gassign *stmt,
    7521              :                            enum tree_code code2, tree op2a, tree op2b,
    7522              :                            basic_block outer_cond_bb)
    7523              : {
    7524       139583 :   tree var = gimple_assign_lhs (stmt);
    7525       139583 :   tree true_test_var = NULL_TREE;
    7526       139583 :   tree false_test_var = NULL_TREE;
    7527       139583 :   enum tree_code innercode = gimple_assign_rhs_code (stmt);
    7528              : 
    7529              :   /* Check for identities like (var AND (var == 0)) => false.  */
    7530       139583 :   if (TREE_CODE (op2a) == SSA_NAME
    7531       139583 :       && TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE)
    7532              :     {
    7533         6236 :       if ((code2 == NE_EXPR && integer_zerop (op2b))
    7534        14612 :           || (code2 == EQ_EXPR && integer_nonzerop (op2b)))
    7535              :         {
    7536         6769 :           true_test_var = op2a;
    7537         6769 :           if (var == true_test_var)
    7538              :             return var;
    7539              :         }
    7540         3114 :       else if ((code2 == EQ_EXPR && integer_zerop (op2b))
    7541         9295 :                || (code2 == NE_EXPR && integer_nonzerop (op2b)))
    7542              :         {
    7543         1312 :           false_test_var = op2a;
    7544         1312 :           if (var == false_test_var)
    7545            0 :             return boolean_false_node;
    7546              :         }
    7547              :     }
    7548              : 
    7549              :   /* If the definition is a comparison, recurse on it.  */
    7550       139583 :   if (TREE_CODE_CLASS (innercode) == tcc_comparison)
    7551              :     {
    7552          500 :       tree t = and_comparisons_1 (type, code, innercode,
    7553              :                                   gimple_assign_rhs1 (stmt),
    7554              :                                   gimple_assign_rhs2 (stmt),
    7555              :                                   code2,
    7556              :                                   op2a,
    7557              :                                   op2b, outer_cond_bb);
    7558          250 :       if (t)
    7559              :         return t;
    7560              :     }
    7561              : 
    7562              :   /* If the definition is an AND or OR expression, we may be able to
    7563              :      simplify by reassociating.  */
    7564       139581 :   if (TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE
    7565       139581 :       && (innercode == BIT_AND_EXPR || innercode == BIT_IOR_EXPR))
    7566              :     {
    7567         9440 :       tree inner1 = gimple_assign_rhs1 (stmt);
    7568         9440 :       tree inner2 = gimple_assign_rhs2 (stmt);
    7569         9440 :       tree t;
    7570         9440 :       tree partial = NULL_TREE;
    7571         9440 :       bool is_and = (innercode == BIT_AND_EXPR);
    7572              : 
    7573              :       /* Check for boolean identities that don't require recursive examination
    7574              :          of inner1/inner2:
    7575              :          inner1 AND (inner1 AND inner2) => inner1 AND inner2 => var
    7576              :          inner1 AND (inner1 OR inner2) => inner1
    7577              :          !inner1 AND (inner1 AND inner2) => false
    7578              :          !inner1 AND (inner1 OR inner2) => !inner1 AND inner2
    7579              :          Likewise for similar cases involving inner2.  */
    7580         9440 :       if (inner1 == true_test_var)
    7581            0 :         return (is_and ? var : inner1);
    7582         9440 :       else if (inner2 == true_test_var)
    7583            0 :         return (is_and ? var : inner2);
    7584         9440 :       else if (inner1 == false_test_var)
    7585            0 :         return (is_and
    7586            0 :                 ? boolean_false_node
    7587            0 :                 : and_var_with_comparison (type, code, inner2, false, code2,
    7588            0 :                                            op2a, op2b, outer_cond_bb));
    7589         9440 :       else if (inner2 == false_test_var)
    7590            0 :         return (is_and
    7591            0 :                 ? boolean_false_node
    7592            0 :                 : and_var_with_comparison (type, code, inner1, false, code2,
    7593            0 :                                            op2a, op2b, outer_cond_bb));
    7594              : 
    7595              :       /* Next, redistribute/reassociate the AND across the inner tests.
    7596              :          Compute the first partial result, (inner1 AND (op2a code op2b))  */
    7597         9440 :       gassign *s;
    7598         9440 :       if (TREE_CODE (inner1) == SSA_NAME
    7599         9440 :           && (s = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (inner1)))
    7600         9027 :           && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
    7601        17363 :           && (t = maybe_fold_and_comparisons (type, code,
    7602              :                                               gimple_assign_rhs_code (s),
    7603              :                                               gimple_assign_rhs1 (s),
    7604              :                                               gimple_assign_rhs2 (s),
    7605              :                                               code2, op2a, op2b,
    7606              :                                               outer_cond_bb)))
    7607              :         {
    7608              :           /* Handle the AND case, where we are reassociating:
    7609              :              (inner1 AND inner2) AND (op2a code2 op2b)
    7610              :              => (t AND inner2)
    7611              :              If the partial result t is a constant, we win.  Otherwise
    7612              :              continue on to try reassociating with the other inner test.  */
    7613           14 :           if (is_and)
    7614              :             {
    7615            0 :               if (integer_onep (t))
    7616              :                 return inner2;
    7617            0 :               else if (integer_zerop (t))
    7618            0 :                 return boolean_false_node;
    7619              :             }
    7620              : 
    7621              :           /* Handle the OR case, where we are redistributing:
    7622              :              (inner1 OR inner2) AND (op2a code2 op2b)
    7623              :              => (t OR (inner2 AND (op2a code2 op2b)))  */
    7624           14 :           else if (integer_onep (t))
    7625            0 :             return boolean_true_node;
    7626              : 
    7627              :           /* Save partial result for later.  */
    7628              :           partial = t;
    7629              :         }
    7630              : 
    7631              :       /* Compute the second partial result, (inner2 AND (op2a code op2b)) */
    7632         9440 :       if (TREE_CODE (inner2) == SSA_NAME
    7633         9440 :           && (s = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (inner2)))
    7634         9189 :           && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
    7635        17535 :           && (t = maybe_fold_and_comparisons (type, code,
    7636              :                                               gimple_assign_rhs_code (s),
    7637              :                                               gimple_assign_rhs1 (s),
    7638              :                                               gimple_assign_rhs2 (s),
    7639              :                                               code2, op2a, op2b,
    7640              :                                               outer_cond_bb)))
    7641              :         {
    7642              :           /* Handle the AND case, where we are reassociating:
    7643              :              (inner1 AND inner2) AND (op2a code2 op2b)
    7644              :              => (inner1 AND t)  */
    7645           49 :           if (is_and)
    7646              :             {
    7647           27 :               if (integer_onep (t))
    7648              :                 return inner1;
    7649           27 :               else if (integer_zerop (t))
    7650            1 :                 return boolean_false_node;
    7651              :               /* If both are the same, we can apply the identity
    7652              :                  (x AND x) == x.  */
    7653           26 :               else if (partial && same_bool_result_p (t, partial))
    7654            0 :                 return t;
    7655              :             }
    7656              : 
    7657              :           /* Handle the OR case. where we are redistributing:
    7658              :              (inner1 OR inner2) AND (op2a code2 op2b)
    7659              :              => (t OR (inner1 AND (op2a code2 op2b)))
    7660              :              => (t OR partial)  */
    7661              :           else
    7662              :             {
    7663           22 :               if (integer_onep (t))
    7664            0 :                 return boolean_true_node;
    7665           22 :               else if (partial)
    7666              :                 {
    7667              :                   /* We already got a simplification for the other
    7668              :                      operand to the redistributed OR expression.  The
    7669              :                      interesting case is when at least one is false.
    7670              :                      Or, if both are the same, we can apply the identity
    7671              :                      (x OR x) == x.  */
    7672            7 :                   if (integer_zerop (partial))
    7673              :                     return t;
    7674            2 :                   else if (integer_zerop (t))
    7675              :                     return partial;
    7676            0 :                   else if (same_bool_result_p (t, partial))
    7677            0 :                     return t;
    7678              :                 }
    7679              :             }
    7680              :         }
    7681              :     }
    7682              :   return NULL_TREE;
    7683              : }
    7684              : 
    7685              : /* Try to simplify the AND of two comparisons defined by
    7686              :    (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
    7687              :    If this can be done without constructing an intermediate value,
    7688              :    return the resulting tree; otherwise NULL_TREE is returned.
    7689              :    This function is deliberately asymmetric as it recurses on SSA_DEFs
    7690              :    in the first comparison but not the second.  CODE is as for
    7691              :    maybe_fold_and_comparisons.  */
    7692              : 
    7693              : static tree
    7694       613674 : and_comparisons_1 (tree type, enum tree_code code,
    7695              :                    enum tree_code code1, tree op1a, tree op1b,
    7696              :                    enum tree_code code2, tree op2a, tree op2b,
    7697              :                    basic_block outer_cond_bb)
    7698              : {
    7699       613674 :   tree truth_type = truth_type_for (TREE_TYPE (op1a));
    7700              : 
    7701              :   /* First check for ((x CODE1 y) AND (x CODE2 y)).  */
    7702       613674 :   if (operand_equal_p (op1a, op2a, 0)
    7703       613674 :       && operand_equal_p (op1b, op2b, 0))
    7704              :     {
    7705              :       /* Result will be either NULL_TREE, or a combined comparison.  */
    7706         3193 :       tree t = combine_comparisons (UNKNOWN_LOCATION,
    7707              :                                     code, code1, code2,
    7708              :                                     truth_type, truth_type, op1a, op1b);
    7709         3193 :       if (t)
    7710              :         return t;
    7711              :     }
    7712              : 
    7713              :   /* Likewise the swapped case of the above.  */
    7714       612081 :   if (operand_equal_p (op1a, op2b, 0)
    7715       612081 :       && operand_equal_p (op1b, op2a, 0))
    7716              :     {
    7717              :       /* Result will be either NULL_TREE, or a combined comparison.  */
    7718            5 :       tree t = combine_comparisons (UNKNOWN_LOCATION,
    7719              :                                     code, code1,
    7720              :                                     swap_tree_comparison (code2),
    7721              :                                     truth_type, truth_type, op1a, op1b);
    7722            5 :       if (t)
    7723              :         return t;
    7724              :     }
    7725              : 
    7726              :   /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
    7727              :      NAME's definition is a truth value.  See if there are any simplifications
    7728              :      that can be done against the NAME's definition.  */
    7729              :   /* ???  Do the same for op2a, but as we are recursing we have to track
    7730              :      whether we swap the order of the comparsions since that's important
    7731              :      with respect to exceptions.  */
    7732       612080 :   if (TREE_CODE (op1a) == SSA_NAME
    7733       612068 :       && (code1 == NE_EXPR || code1 == EQ_EXPR)
    7734      1097230 :       && (integer_zerop (op1b) || integer_onep (op1b)))
    7735              :     {
    7736        94486 :       bool invert = ((code1 == EQ_EXPR && integer_zerop (op1b))
    7737       237678 :                      || (code1 == NE_EXPR && integer_onep (op1b)));
    7738       217783 :       gimple *stmt = SSA_NAME_DEF_STMT (op1a);
    7739       217783 :       switch (gimple_code (stmt))
    7740              :         {
    7741       182029 :         case GIMPLE_ASSIGN:
    7742              :           /* Try to simplify by copy-propagating the definition.  */
    7743       182029 :           return and_var_with_comparison (type, code, op1a, invert, code2,
    7744       182029 :                                           op2a, op2b, outer_cond_bb);
    7745              : 
    7746        15211 :         case GIMPLE_PHI:
    7747              :           /* If every argument to the PHI produces the same result when
    7748              :              ANDed with the second comparison, we win.
    7749              :              Do not do this unless the type is bool since we need a bool
    7750              :              result here anyway.  */
    7751        15211 :           if (TREE_CODE (TREE_TYPE (op1a)) == BOOLEAN_TYPE)
    7752              :             {
    7753              :               tree result = NULL_TREE;
    7754              :               unsigned i;
    7755         6885 :               for (i = 0; i < gimple_phi_num_args (stmt); i++)
    7756              :                 {
    7757         6885 :                   tree arg = gimple_phi_arg_def (stmt, i);
    7758              : 
    7759              :                   /* If this PHI has itself as an argument, ignore it.
    7760              :                      If all the other args produce the same result,
    7761              :                      we're still OK.  */
    7762         6885 :                   if (arg == gimple_phi_result (stmt))
    7763            0 :                     continue;
    7764         6885 :                   else if (TREE_CODE (arg) == INTEGER_CST)
    7765              :                     {
    7766         5504 :                       if (invert ? integer_nonzerop (arg) : integer_zerop (arg))
    7767              :                         {
    7768         3541 :                           if (!result)
    7769         1218 :                             result = boolean_false_node;
    7770         2323 :                           else if (!integer_zerop (result))
    7771              :                             return NULL_TREE;
    7772              :                         }
    7773         1963 :                       else if (!result)
    7774         1069 :                         result = fold_build2 (code2, boolean_type_node,
    7775              :                                               op2a, op2b);
    7776          894 :                       else if (!same_bool_comparison_p (result,
    7777              :                                                         code2, op2a, op2b))
    7778              :                         return NULL_TREE;
    7779              :                     }
    7780         1381 :                   else if (TREE_CODE (arg) == SSA_NAME
    7781         1381 :                            && !SSA_NAME_IS_DEFAULT_DEF (arg))
    7782              :                     {
    7783         1378 :                       tree temp;
    7784         1378 :                       gimple *def_stmt = SSA_NAME_DEF_STMT (arg);
    7785              :                       /* In simple cases we can look through PHI nodes,
    7786              :                          but we have to be careful with loops.
    7787              :                          See PR49073.  */
    7788         1378 :                       if (! dom_info_available_p (CDI_DOMINATORS)
    7789         1378 :                           || gimple_bb (def_stmt) == gimple_bb (stmt)
    7790         2756 :                           || dominated_by_p (CDI_DOMINATORS,
    7791         1378 :                                              gimple_bb (def_stmt),
    7792         1378 :                                              gimple_bb (stmt)))
    7793              :                         return NULL_TREE;
    7794          999 :                       temp = and_var_with_comparison (type, code, arg, invert,
    7795              :                                                       code2, op2a, op2b,
    7796              :                                                       outer_cond_bb);
    7797          999 :                       if (!temp)
    7798              :                         return NULL_TREE;
    7799            0 :                       else if (!result)
    7800              :                         result = temp;
    7801            0 :                       else if (!same_bool_result_p (result, temp))
    7802              :                         return NULL_TREE;
    7803              :                     }
    7804              :                   else
    7805              :                     return NULL_TREE;
    7806              :                 }
    7807              :               return result;
    7808              :             }
    7809              : 
    7810              :         default:
    7811              :           break;
    7812              :         }
    7813              :     }
    7814              :   return NULL_TREE;
    7815              : }
    7816              : 
    7817              : static basic_block fosa_bb;
    7818              : static vec<std::pair<tree, flow_sensitive_info_storage> > *fosa_unwind;
    7819              : static tree
    7820     53319270 : follow_outer_ssa_edges (tree val)
    7821              : {
    7822     53319270 :   if (TREE_CODE (val) == SSA_NAME
    7823     53319270 :       && !SSA_NAME_IS_DEFAULT_DEF (val))
    7824              :     {
    7825     52582364 :       basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (val));
    7826     52582364 :       if (!def_bb
    7827     18490813 :           || def_bb == fosa_bb
    7828     62264229 :           || (dom_info_available_p (CDI_DOMINATORS)
    7829      9681865 :               && (def_bb == fosa_bb
    7830      9681865 :                   || dominated_by_p (CDI_DOMINATORS, fosa_bb, def_bb))))
    7831              :         return val;
    7832      6950591 :       flow_sensitive_info_storage storage;
    7833      6950591 :       storage.save_and_clear (val);
    7834              :       /* If the definition does not dominate fosa_bb temporarily reset
    7835              :          flow-sensitive info.  */
    7836      6950591 :       fosa_unwind->safe_push (std::make_pair (val, storage));
    7837              :       /* We cannot temporarily rewrite stmts with undefined overflow
    7838              :          behavior, so avoid expanding them. But still save off the
    7839              :          flow-sensitive info as we might be using the ssa name as the leaf.  */
    7840     13863241 :       if ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (val))
    7841       847239 :            || POINTER_TYPE_P (TREE_TYPE (val)))
    7842     13443267 :           && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (val)))
    7843      3986880 :         return NULL_TREE;
    7844              :       return val;
    7845              :     }
    7846              :   return val;
    7847              : }
    7848              : 
    7849              : /* Helper function for maybe_fold_and_comparisons and maybe_fold_or_comparisons
    7850              :    : try to simplify the AND/OR of the ssa variable VAR with the comparison
    7851              :    specified by (OP2A CODE2 OP2B) from match.pd.  Return NULL_EXPR if we can't
    7852              :    simplify this to a single expression.  As we are going to lower the cost
    7853              :    of building SSA names / gimple stmts significantly, we need to allocate
    7854              :    them ont the stack.  This will cause the code to be a bit ugly.  */
    7855              : 
    7856              : static tree
    7857       848188 : maybe_fold_comparisons_from_match_pd (tree type, enum tree_code code,
    7858              :                                       enum tree_code code1,
    7859              :                                       tree op1a, tree op1b,
    7860              :                                       enum tree_code code2, tree op2a,
    7861              :                                       tree op2b,
    7862              :                                       basic_block outer_cond_bb)
    7863              : {
    7864              :   /* Allocate gimple stmt1 on the stack.  */
    7865       848188 :   gassign *stmt1
    7866       848188 :     = (gassign *) XALLOCAVEC (char, gimple_size (GIMPLE_ASSIGN, 3));
    7867       848188 :   gimple_init (stmt1, GIMPLE_ASSIGN, 3);
    7868       848188 :   gimple_assign_set_rhs_code (stmt1, code1);
    7869       848188 :   gimple_assign_set_rhs1 (stmt1, op1a);
    7870       848188 :   gimple_assign_set_rhs2 (stmt1, op1b);
    7871       848188 :   gimple_set_bb (stmt1, NULL);
    7872              : 
    7873              :   /* Allocate gimple stmt2 on the stack.  */
    7874       848188 :   gassign *stmt2
    7875       848188 :     = (gassign *) XALLOCAVEC (char, gimple_size (GIMPLE_ASSIGN, 3));
    7876       848188 :   gimple_init (stmt2, GIMPLE_ASSIGN, 3);
    7877       848188 :   gimple_assign_set_rhs_code (stmt2, code2);
    7878       848188 :   gimple_assign_set_rhs1 (stmt2, op2a);
    7879       848188 :   gimple_assign_set_rhs2 (stmt2, op2b);
    7880       848188 :   gimple_set_bb (stmt2, NULL);
    7881              : 
    7882              :   /* Allocate SSA names(lhs1) on the stack.  */
    7883       848188 :   alignas (tree_node) unsigned char lhs1buf[sizeof (tree_ssa_name)];
    7884       848188 :   tree lhs1 = (tree) &lhs1buf[0];
    7885       848188 :   memset (lhs1, 0, sizeof (tree_ssa_name));
    7886       848188 :   TREE_SET_CODE (lhs1, SSA_NAME);
    7887       848188 :   TREE_TYPE (lhs1) = type;
    7888       848188 :   init_ssa_name_imm_use (lhs1);
    7889              : 
    7890              :   /* Allocate SSA names(lhs2) on the stack.  */
    7891       848188 :   alignas (tree_node) unsigned char lhs2buf[sizeof (tree_ssa_name)];
    7892       848188 :   tree lhs2 = (tree) &lhs2buf[0];
    7893       848188 :   memset (lhs2, 0, sizeof (tree_ssa_name));
    7894       848188 :   TREE_SET_CODE (lhs2, SSA_NAME);
    7895       848188 :   TREE_TYPE (lhs2) = type;
    7896       848188 :   init_ssa_name_imm_use (lhs2);
    7897              : 
    7898       848188 :   gimple_assign_set_lhs (stmt1, lhs1);
    7899       848188 :   gimple_assign_set_lhs (stmt2, lhs2);
    7900              : 
    7901       848188 :   gimple_match_op op (gimple_match_cond::UNCOND, code,
    7902              :                       type, gimple_assign_lhs (stmt1),
    7903       848188 :                       gimple_assign_lhs (stmt2));
    7904       848188 :   fosa_bb = outer_cond_bb;
    7905       848188 :   auto_vec<std::pair<tree, flow_sensitive_info_storage>, 8> unwind_stack;
    7906       848188 :   fosa_unwind = &unwind_stack;
    7907      1346011 :   if (op.resimplify (NULL, (!outer_cond_bb
    7908              :                             ? follow_all_ssa_edges : follow_outer_ssa_edges)))
    7909              :     {
    7910         2185 :       fosa_unwind = NULL;
    7911         7978 :       for (auto p : unwind_stack)
    7912         1423 :         p.second.restore (p.first);
    7913         2185 :       if (gimple_simplified_result_is_gimple_val (&op))
    7914              :         {
    7915         1307 :           tree res = op.ops[0];
    7916         1307 :           if (res == lhs1)
    7917           53 :             return build2 (code1, type, op1a, op1b);
    7918         1254 :           else if (res == lhs2)
    7919         1230 :             return build2 (code2, type, op2a, op2b);
    7920              :           else
    7921              :             return res;
    7922              :         }
    7923          878 :       else if (op.code.is_tree_code ()
    7924          878 :                && TREE_CODE_CLASS ((tree_code)op.code) == tcc_comparison)
    7925              :         {
    7926          878 :           tree op0 = op.ops[0];
    7927          878 :           tree op1 = op.ops[1];
    7928          878 :           if (op0 == lhs1 || op0 == lhs2 || op1 == lhs1 || op1 == lhs2)
    7929              :             return NULL_TREE;  /* not simple */
    7930              : 
    7931          878 :           return build2 ((enum tree_code)op.code, op.type, op0, op1);
    7932              :         }
    7933              :     }
    7934       846003 :   fosa_unwind = NULL;
    7935      9487177 :   for (auto p : unwind_stack)
    7936      6949168 :     p.second.restore (p.first);
    7937              : 
    7938       846003 :   return NULL_TREE;
    7939       848188 : }
    7940              : 
    7941              : /* Return TRUE and set op[0] if T, following all SSA edges, is a type
    7942              :    conversion.  Reject loads if LOAD is NULL, otherwise set *LOAD if a
    7943              :    converting load is found.  */
    7944              : 
    7945              : static bool
    7946      3355010 : gimple_convert_def_p (tree t, tree op[1], gimple **load = NULL)
    7947              : {
    7948      3355010 :   bool ret = false;
    7949              : 
    7950      3355010 :   if (TREE_CODE (t) == SSA_NAME
    7951      3355010 :       && !SSA_NAME_IS_DEFAULT_DEF (t))
    7952      1864533 :     if (gassign *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (t)))
    7953              :       {
    7954      1769706 :         bool load_p = gimple_assign_load_p (def);
    7955      1769706 :         if (load_p && !load)
    7956              :           return false;
    7957      1756832 :         switch (gimple_assign_rhs_code (def))
    7958              :           {
    7959        14805 :           CASE_CONVERT:
    7960        14805 :             op[0] = gimple_assign_rhs1 (def);
    7961        14805 :             ret = true;
    7962        14805 :             break;
    7963              : 
    7964         4093 :           case VIEW_CONVERT_EXPR:
    7965         4093 :             op[0] = TREE_OPERAND (gimple_assign_rhs1 (def), 0);
    7966         4093 :             ret = true;
    7967         4093 :             break;
    7968              : 
    7969              :           default:
    7970              :             break;
    7971              :           }
    7972              : 
    7973        18898 :         if (ret && load_p)
    7974            0 :           *load = def;
    7975              :       }
    7976              : 
    7977              :   return ret;
    7978              : }
    7979              : 
    7980              : /* Return TRUE and set op[*] if T, following all SSA edges, resolves to a
    7981              :    binary expression with code CODE.  */
    7982              : 
    7983              : static bool
    7984      3391419 : gimple_binop_def_p (enum tree_code code, tree t, tree op[2])
    7985              : {
    7986      3391419 :   if (TREE_CODE (t) == SSA_NAME
    7987      3391419 :       && !SSA_NAME_IS_DEFAULT_DEF (t))
    7988      1898357 :     if (gimple *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (t)))
    7989      1800913 :       if (gimple_assign_rhs_code (def) == code)
    7990              :         {
    7991        59098 :           op[0] = gimple_assign_rhs1 (def);
    7992        59098 :           op[1] = gimple_assign_rhs2 (def);
    7993        59098 :           return true;
    7994              :         }
    7995              :   return false;
    7996              : }
    7997              : /* Subroutine for fold_truth_andor_1: decode a field reference.
    7998              : 
    7999              :    If *PEXP is a comparison reference, we return the innermost reference.
    8000              : 
    8001              :    *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
    8002              :    set to the starting bit number.
    8003              : 
    8004              :    *PVOLATILEP is set to 1 if the any expression encountered is volatile;
    8005              :    otherwise it is not changed.
    8006              : 
    8007              :    *PUNSIGNEDP is set to the signedness of the field.
    8008              : 
    8009              :    *PREVERSEP is set to the storage order of the field.
    8010              : 
    8011              :    *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.  If
    8012              :    *PAND_MASK is initially set to a mask with nonzero precision, that mask is
    8013              :    combined with the found mask, or adjusted in precision to match.
    8014              : 
    8015              :    *PSIGNBIT is set to 1 if, before clipping to *PBITSIZE, the mask
    8016              :    encompassed bits that corresponded to extensions of the sign bit, or to -1
    8017              :    if the mask encompassed bits that would be extensions of the sign bit
    8018              :    and zeroed them out.  Otherwise, it is set to 0.
    8019              : 
    8020              :    *PXORP is to be FALSE if EXP might be a XOR used in a compare, in which
    8021              :    case, if PXOR_CMP_OP is a zero constant, it will be overridden with *PEXP,
    8022              :    *PXORP will be set to TRUE, *PXOR_AND_MASK will be copied from *PAND_MASK,
    8023              :    and the left-hand operand of the XOR will be decoded.  If *PXORP is TRUE,
    8024              :    PXOR_CMP_OP and PXOR_AND_MASK are supposed to be NULL, and then the
    8025              :    right-hand operand of the XOR will be decoded.
    8026              : 
    8027              :    *LOAD is set to the load stmt of the innermost reference, if any,
    8028              :    *and NULL otherwise.
    8029              : 
    8030              :    LOC[0..3] are filled in as conversion, masking, shifting and loading
    8031              :    operations are located.
    8032              : 
    8033              :    Return 0 if this is not a component reference or is one that we can't
    8034              :    do anything with.  */
    8035              : 
    8036              : static tree
    8037      1201830 : decode_field_reference (tree *pexp, HOST_WIDE_INT *pbitsize,
    8038              :                         HOST_WIDE_INT *pbitpos,
    8039              :                         bool *punsignedp, bool *preversep, bool *pvolatilep,
    8040              :                         wide_int *pand_mask, int *psignbit,
    8041              :                         bool *pxorp, tree *pxor_cmp_op, wide_int *pxor_and_mask,
    8042              :                         gimple **pload, location_t loc[4])
    8043              : {
    8044      1201830 :   tree exp = *pexp;
    8045      1201830 :   tree outer_type = 0;
    8046      1201830 :   wide_int and_mask;
    8047      1201830 :   tree inner, offset;
    8048      1201830 :   int shiftrt = 0;
    8049      1201830 :   tree res_ops[2];
    8050      1201830 :   machine_mode mode;
    8051      1201830 :   bool convert_before_shift = false;
    8052      1201830 :   int signbit = 0;
    8053      1201830 :   bool xorp = false;
    8054      1201830 :   tree xor_cmp_op;
    8055      1201830 :   wide_int xor_and_mask;
    8056      1201830 :   gimple *load = NULL;
    8057              : 
    8058              :   /* All the optimizations using this function assume integer fields.
    8059              :      There are problems with FP fields since the type_for_size call
    8060              :      below can fail for, e.g., XFmode.  */
    8061      1201830 :   if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
    8062              :     return NULL_TREE;
    8063              : 
    8064              :   /* Drop casts, saving only the outermost type, effectively used in
    8065              :      the compare.  We can deal with at most one conversion, and it may
    8066              :      appear at various points in the chain of recognized preparation
    8067              :      statements.  Earlier optimizers will often have already dropped
    8068              :      unneeded extensions, but they may survive, as in PR118046.  ???
    8069              :      Can we do better and allow multiple conversions, perhaps taking
    8070              :      note of the narrowest intermediate type, sign extensions and
    8071              :      whatnot?  */
    8072      1130491 :   if (!outer_type && gimple_convert_def_p (exp, res_ops))
    8073              :     {
    8074        17477 :       outer_type = TREE_TYPE (exp);
    8075        17477 :       loc[0] = gimple_location (SSA_NAME_DEF_STMT (exp));
    8076        17477 :       exp = res_ops[0];
    8077              :     }
    8078              : 
    8079              :   /* Recognize and save a masking operation.  Combine it with an
    8080              :      incoming mask.  */
    8081      1130491 :   if (gimple_binop_def_p (BIT_AND_EXPR, exp, res_ops)
    8082      1130491 :       && TREE_CODE (res_ops[1]) == INTEGER_CST)
    8083              :     {
    8084        34364 :       loc[1] = gimple_location (SSA_NAME_DEF_STMT (exp));
    8085        34364 :       exp = res_ops[0];
    8086        34364 :       and_mask = wi::to_wide (res_ops[1]);
    8087        34364 :       unsigned prec_in = pand_mask->get_precision ();
    8088        34364 :       if (prec_in)
    8089              :         {
    8090           18 :           unsigned prec_op = and_mask.get_precision ();
    8091           18 :           if (prec_in >= prec_op)
    8092              :             {
    8093           18 :               if (prec_in > prec_op)
    8094           28 :                 and_mask = wide_int::from (and_mask, prec_in,
    8095           28 :                                            TYPE_SIGN (TREE_TYPE (res_ops[1])));
    8096           18 :               and_mask &= *pand_mask;
    8097              :             }
    8098              :           else
    8099            0 :             and_mask &= wide_int::from (*pand_mask, prec_op, UNSIGNED);
    8100              :         }
    8101              :       /* If we have already dropped a narrowing conversion, it applies to
    8102              :          the result of the masking, so mask bits at or above the precision
    8103              :          of OUTER_TYPE are discarded by the conversion.  Drop them from the
    8104              :          mask, so that they're not mistaken for tests of extensions of the
    8105              :          sign bit below.  Bits below that precision but above the field
    8106              :          width, e.g. after a right shift, still test such extensions.  */
    8107        34364 :       if (outer_type
    8108        34364 :           && TYPE_PRECISION (outer_type) < and_mask.get_precision ())
    8109           76 :         and_mask = wide_int::from (and_mask, TYPE_PRECISION (outer_type),
    8110           38 :                                    UNSIGNED);
    8111              :     }
    8112              :   else
    8113      1096127 :     and_mask = *pand_mask;
    8114              : 
    8115              :   /* Turn (a ^ b) [!]= 0 into a [!]= b.  */
    8116      1130491 :   if (pxorp && gimple_binop_def_p (BIT_XOR_EXPR, exp, res_ops))
    8117              :     {
    8118              :       /* No location recorded for this one, it's entirely subsumed by the
    8119              :          compare.  */
    8120        11119 :       if (*pxorp)
    8121              :         {
    8122         5531 :           exp = res_ops[1];
    8123         5531 :           gcc_checking_assert (!pxor_cmp_op && !pxor_and_mask);
    8124         5531 :           if (outer_type && TREE_CODE (exp) == INTEGER_CST)
    8125            8 :             exp = fold_convert (outer_type, exp);
    8126              :         }
    8127         5588 :       else if (!pxor_cmp_op)
    8128              :         /* Not much we can do when xor appears in the right-hand compare
    8129              :            operand.  */
    8130              :         return NULL_TREE;
    8131         5534 :       else if (integer_zerop (*pxor_cmp_op))
    8132              :         {
    8133         5531 :           xorp = true;
    8134         5531 :           exp = res_ops[0];
    8135         5531 :           xor_cmp_op = *pexp;
    8136         5531 :           xor_and_mask = *pand_mask;
    8137              :         }
    8138              :     }
    8139              : 
    8140              :   /* Another chance to drop conversions.  */
    8141      1130437 :   if (!outer_type && gimple_convert_def_p (exp, res_ops))
    8142              :     {
    8143         1401 :       outer_type = TREE_TYPE (exp);
    8144         1401 :       loc[0] = gimple_location (SSA_NAME_DEF_STMT (exp));
    8145         1401 :       exp = res_ops[0];
    8146              :     }
    8147              : 
    8148              :   /* Take note of shifts.  */
    8149      1130437 :   if (gimple_binop_def_p (RSHIFT_EXPR, exp, res_ops)
    8150      1130437 :       && TREE_CODE (res_ops[1]) == INTEGER_CST)
    8151              :     {
    8152          659 :       loc[2] = gimple_location (SSA_NAME_DEF_STMT (exp));
    8153          659 :       exp = res_ops[0];
    8154          659 :       if (!tree_fits_shwi_p (res_ops[1]))
    8155              :         return NULL_TREE;
    8156          659 :       shiftrt = tree_to_shwi (res_ops[1]);
    8157          659 :       if (shiftrt <= 0)
    8158              :         return NULL_TREE;
    8159              :     }
    8160              : 
    8161              :   /* Yet another chance to drop conversions.  This one is allowed to
    8162              :      match a converting load, subsuming the load identification block
    8163              :      below.  */
    8164      1130437 :   if (!outer_type && gimple_convert_def_p (exp, res_ops, &load))
    8165              :     {
    8166           20 :       outer_type = TREE_TYPE (exp);
    8167           20 :       loc[0] = gimple_location (SSA_NAME_DEF_STMT (exp));
    8168           20 :       if (load)
    8169            0 :         loc[3] = gimple_location (load);
    8170           20 :       exp = res_ops[0];
    8171              :       /* This looks backwards, but we're going back the def chain, so if we
    8172              :          find the conversion here, after finding a shift, that's because the
    8173              :          convert appears before the shift, and we should thus adjust the bit
    8174              :          pos and size because of the shift after adjusting it due to type
    8175              :          conversion.  */
    8176           20 :       convert_before_shift = true;
    8177              :     }
    8178              : 
    8179              :   /* Identify the load, if there is one.  */
    8180      1130437 :   if (!load && TREE_CODE (exp) == SSA_NAME && !SSA_NAME_IS_DEFAULT_DEF (exp))
    8181              :     {
    8182       631903 :       gimple *def = SSA_NAME_DEF_STMT (exp);
    8183       631903 :       if (gimple_assign_load_p (def))
    8184              :         {
    8185       405768 :           loc[3] = gimple_location (def);
    8186       405768 :           load = def;
    8187       405768 :           exp = gimple_assign_rhs1 (def);
    8188              :         }
    8189              :     }
    8190              : 
    8191              :   /* Identify the relevant bits.  */
    8192      1130437 :   poly_int64 poly_bitsize, poly_bitpos;
    8193      1130437 :   int unsignedp, reversep = *preversep, volatilep = *pvolatilep;
    8194      1130437 :   inner = get_inner_reference (exp, &poly_bitsize, &poly_bitpos, &offset,
    8195              :                                &mode, &unsignedp, &reversep, &volatilep);
    8196              : 
    8197      1130437 :   HOST_WIDE_INT bs, bp;
    8198      1130437 :   if (!poly_bitsize.is_constant (&bs)
    8199      1130437 :       || !poly_bitpos.is_constant (&bp)
    8200      1130437 :       || bs <= shiftrt
    8201      1130437 :       || offset != 0
    8202      1129147 :       || TREE_CODE (inner) == PLACEHOLDER_EXPR
    8203              :       /* Reject out-of-bound accesses (PR79731, PR118514).  */
    8204      1180291 :       || !access_in_bounds_of_type_p (TREE_TYPE (inner), bs, bp)
    8205      1129120 :       || (INTEGRAL_TYPE_P (TREE_TYPE (inner))
    8206       768301 :           && !type_has_mode_precision_p (TREE_TYPE (inner))))
    8207              :     return NULL_TREE;
    8208              : 
    8209              :   /* Adjust shifts...  */
    8210      1079266 :   if (convert_before_shift
    8211      1079266 :       && outer_type && bs > TYPE_PRECISION (outer_type))
    8212              :     {
    8213            2 :       HOST_WIDE_INT excess = bs - TYPE_PRECISION (outer_type);
    8214            2 :       if (reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
    8215            0 :         bp += excess;
    8216              :       bs -= excess;
    8217              :     }
    8218              : 
    8219      1079266 :   if (shiftrt)
    8220              :     {
    8221              :       /* Punt if we're shifting by more than the loaded bitfield (after
    8222              :          adjustment), or if there's a shift after a change of signedness, punt.
    8223              :          When comparing this field with a constant, we'll check that the
    8224              :          constant is a proper sign- or zero-extension (depending on signedness)
    8225              :          of a value that would fit in the selected portion of the bitfield.  A
    8226              :          shift after a change of signedness would make the extension
    8227              :          non-uniform, and we can't deal with that (yet ???).  See
    8228              :          gcc.dg/field-merge-22.c for a test that would go wrong.  */
    8229          659 :       if (bs <= shiftrt
    8230          659 :           || (convert_before_shift
    8231           20 :               && outer_type && unsignedp != TYPE_UNSIGNED (outer_type)))
    8232              :         return NULL_TREE;
    8233          640 :       if (!reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
    8234          640 :         bp += shiftrt;
    8235          640 :       bs -= shiftrt;
    8236              :     }
    8237              : 
    8238              :   /* ... and bit position.  */
    8239      1079247 :   if (!convert_before_shift
    8240      1079247 :       && outer_type && bs > TYPE_PRECISION (outer_type))
    8241              :     {
    8242         8138 :       HOST_WIDE_INT excess = bs - TYPE_PRECISION (outer_type);
    8243         8138 :       if (reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
    8244            0 :         bp += excess;
    8245              :       bs -= excess;
    8246              :     }
    8247              : 
    8248              :   /* If the number of bits in the reference is the same as the bitsize of
    8249              :      the outer type, then the outer type gives the signedness. Otherwise
    8250              :      (in case of a small bitfield) the signedness is unchanged.  */
    8251      1079247 :   if (outer_type && bs == TYPE_PRECISION (outer_type))
    8252        13909 :     unsignedp = TYPE_UNSIGNED (outer_type);
    8253              : 
    8254              :   /* Make the mask the expected width.  */
    8255      1079247 :   if (and_mask.get_precision () != 0)
    8256              :     {
    8257              :       /* If the AND_MASK encompasses bits that would be extensions of
    8258              :          the sign bit, set SIGNBIT to 1 if any such bit is set, or -1 if
    8259              :          they are all clear.  */
    8260        39679 :       if (!unsignedp
    8261        39679 :           && and_mask.get_precision () > bs)
    8262              :         {
    8263           73 :           if ((and_mask & wi::mask (bs, true, and_mask.get_precision ())) != 0)
    8264              :             signbit = 1;
    8265              :           else
    8266           27 :             signbit = -1;
    8267              :         }
    8268        39679 :       and_mask = wide_int::from (and_mask, bs, UNSIGNED);
    8269              :     }
    8270              : 
    8271      1079247 :   *pexp = exp;
    8272      1079247 :   *pload = load;
    8273      1079247 :   *pbitsize = bs;
    8274      1079247 :   *pbitpos = bp;
    8275      1079247 :   *punsignedp = unsignedp;
    8276      1079247 :   *preversep = reversep;
    8277      1079247 :   *pvolatilep = volatilep;
    8278      1079247 :   *psignbit = signbit;
    8279      1079247 :   *pand_mask = and_mask;
    8280      1079247 :   if (xorp)
    8281              :     {
    8282         5531 :       *pxorp = xorp;
    8283         5531 :       *pxor_cmp_op = xor_cmp_op;
    8284         5531 :       *pxor_and_mask = xor_and_mask;
    8285              :     }
    8286              : 
    8287              :   return inner;
    8288      1201830 : }
    8289              : 
    8290              : /* Return the one bitpos within bit extents L or R that is at an
    8291              :    ALIGN-bit alignment boundary, or -1 if there is more than one such
    8292              :    boundary, if there isn't any, or if there is any such boundary
    8293              :    between the extents.  L and R are given by bitpos and bitsize.  If
    8294              :    it doesn't return -1, there are two consecutive ALIGN-bit words
    8295              :    that contain both extents, and at least one of the extents
    8296              :    straddles across the returned alignment boundary.  */
    8297              : 
    8298              : static inline HOST_WIDE_INT
    8299        50311 : compute_split_boundary_from_align (HOST_WIDE_INT align,
    8300              :                                    HOST_WIDE_INT l_bitpos,
    8301              :                                    HOST_WIDE_INT l_bitsize,
    8302              :                                    HOST_WIDE_INT r_bitpos,
    8303              :                                    HOST_WIDE_INT r_bitsize)
    8304              : {
    8305        50311 :   HOST_WIDE_INT amask = ~(align - 1);
    8306              : 
    8307        50311 :   HOST_WIDE_INT first_bit = MIN (l_bitpos, r_bitpos);
    8308        50311 :   HOST_WIDE_INT end_bit = MAX (l_bitpos + l_bitsize, r_bitpos + r_bitsize);
    8309              : 
    8310        50311 :   HOST_WIDE_INT boundary = (end_bit - 1) & amask;
    8311              : 
    8312              :   /* Make sure we're crossing no more than one alignment boundary.
    8313              : 
    8314              :      ??? We don't have logic to recombine loads of two adjacent
    8315              :      fields that each crosses a different alignment boundary, so
    8316              :      as to load the middle word only once, if other words can't be
    8317              :      otherwise recombined.  */
    8318        50311 :   if (boundary - first_bit > align)
    8319              :     return -1;
    8320              : 
    8321        19316 :   HOST_WIDE_INT l_start_word = l_bitpos & amask;
    8322        19316 :   HOST_WIDE_INT l_end_word = (l_bitpos + l_bitsize - 1) & amask;
    8323              : 
    8324        19316 :   HOST_WIDE_INT r_start_word = r_bitpos & amask;
    8325        19316 :   HOST_WIDE_INT r_end_word = (r_bitpos + r_bitsize - 1) & amask;
    8326              : 
    8327              :   /* If neither field straddles across an alignment boundary, it's no
    8328              :      use to even try to merge them.  */
    8329        19316 :   if (l_start_word == l_end_word && r_start_word == r_end_word)
    8330        19009 :     return -1;
    8331              : 
    8332              :   return boundary;
    8333              : }
    8334              : 
    8335              : /* Make a bit_field_ref.  If POINT is NULL, return the BIT_FIELD_REF.
    8336              :    Otherwise, build and insert a load stmt before POINT, and return
    8337              :    the SSA_NAME.  ???  Rewrite LOAD in terms of the bitfield?  */
    8338              : 
    8339              : static tree
    8340         4881 : make_bit_field_load (location_t loc, tree inner, tree orig_inner, tree type,
    8341              :                      HOST_WIDE_INT bitsize, poly_int64 bitpos,
    8342              :                      bool unsignedp, bool reversep, gimple *point)
    8343              : {
    8344         4881 :   if (point && loc == UNKNOWN_LOCATION)
    8345           22 :     loc = gimple_location (point);
    8346              : 
    8347         4881 :   tree ref = make_bit_field_ref (loc, unshare_expr (inner),
    8348              :                                  unshare_expr (orig_inner),
    8349              :                                  type, bitsize, bitpos,
    8350              :                                  unsignedp, reversep);
    8351         4881 :   if (!point)
    8352              :     return ref;
    8353              : 
    8354              :   /* If we're remaking the same load, reuse the SSA NAME it is already loaded
    8355              :      into.  */
    8356         4677 :   if (gimple_assign_load_p (point)
    8357         4677 :       && operand_equal_p (ref, gimple_assign_rhs1 (point)))
    8358              :     {
    8359         1692 :       gcc_checking_assert (TREE_CODE (gimple_assign_lhs (point)) == SSA_NAME);
    8360              :       return gimple_assign_lhs (point);
    8361              :     }
    8362              : 
    8363         2985 :   gimple_seq stmts = NULL;
    8364         2985 :   tree ret = force_gimple_operand (ref, &stmts, true, NULL_TREE);
    8365              : 
    8366              :   /* We know the vuse is supposed to end up being the same as that at the
    8367              :      original load at the insertion point, but if we don't set it, it will be a
    8368              :      generic placeholder that only the global SSA update at the end of the pass
    8369              :      would make equal, too late for us to use in further combinations.  So go
    8370              :      ahead and copy the vuse.  */
    8371              : 
    8372         2985 :   tree reaching_vuse = gimple_vuse (point);
    8373         2985 :   for (gimple_stmt_iterator i = gsi_start (stmts);
    8374         6629 :        !gsi_end_p (i); gsi_next (&i))
    8375              :     {
    8376         3644 :       gimple *new_stmt = gsi_stmt (i);
    8377         7288 :       if (gimple_has_mem_ops (new_stmt))
    8378         3644 :         gimple_set_vuse (new_stmt, reaching_vuse);
    8379              :     }
    8380              : 
    8381         2985 :   gimple_stmt_iterator gsi = gsi_for_stmt (point);
    8382         2985 :   gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
    8383         2985 :   return ret;
    8384              : }
    8385              : 
    8386              : /* Initialize ln_arg[0] and ln_arg[1] to a pair of newly-created (at
    8387              :    LOC) loads from INNER (from ORIG_INNER), of modes MODE and MODE2,
    8388              :    respectively, starting at BIT_POS, using reversed endianness if
    8389              :    REVERSEP.  Also initialize BITPOS (the starting position of each
    8390              :    part into INNER), BITSIZ (the bit count starting at BITPOS),
    8391              :    TOSHIFT[1] (the amount by which the part and its mask are to be
    8392              :    shifted right to bring its least-significant bit to bit zero) and
    8393              :    SHIFTED (the amount by which the part, by separate loading, has
    8394              :    already been shifted right, but that the mask needs shifting to
    8395              :    match).  */
    8396              : 
    8397              : static inline void
    8398          307 : build_split_load (tree /* out */ ln_arg[2],
    8399              :                   HOST_WIDE_INT /* out */ bitpos[2],
    8400              :                   HOST_WIDE_INT /* out */ bitsiz[2],
    8401              :                   HOST_WIDE_INT /* in[0] out[0..1] */ toshift[2],
    8402              :                   HOST_WIDE_INT /* out */ shifted[2],
    8403              :                   location_t loc, tree inner, tree orig_inner,
    8404              :                   scalar_int_mode mode, scalar_int_mode mode2,
    8405              :                   HOST_WIDE_INT bit_pos, bool reversep,
    8406              :                   gimple *point[2])
    8407              : {
    8408          307 :   scalar_int_mode modes[2] = { mode, mode2 };
    8409          307 :   bitsiz[0] = GET_MODE_BITSIZE (mode);
    8410          307 :   bitsiz[1] = GET_MODE_BITSIZE (mode2);
    8411              : 
    8412          921 :   for (int i = 0; i < 2; i++)
    8413              :     {
    8414          614 :       tree type = lang_hooks.types.type_for_mode (modes[i], 1);
    8415          614 :       if (!type)
    8416              :         {
    8417            0 :           type = build_nonstandard_integer_type (bitsiz[0], 1);
    8418            0 :           gcc_assert (type);
    8419              :         }
    8420          614 :       bitpos[i] = bit_pos;
    8421         1228 :       ln_arg[i] = make_bit_field_load (loc, inner, orig_inner,
    8422          614 :                                        type, bitsiz[i],
    8423          614 :                                        bit_pos, 1, reversep, point[i]);
    8424          614 :       bit_pos += bitsiz[i];
    8425              :     }
    8426              : 
    8427          307 :   toshift[1] = toshift[0];
    8428          307 :   if (reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
    8429              :     {
    8430            3 :       shifted[0] = bitsiz[1];
    8431            3 :       shifted[1] = 0;
    8432            3 :       toshift[0] = 0;
    8433              :     }
    8434              :   else
    8435              :     {
    8436          304 :       shifted[1] = bitsiz[0];
    8437          304 :       shifted[0] = 0;
    8438          304 :       toshift[1] = 0;
    8439              :     }
    8440          307 : }
    8441              : 
    8442              : /* Make arrangements to split at bit BOUNDARY a single loaded word
    8443              :    (with REVERSEP bit order) LN_ARG[0], to be shifted right by
    8444              :    TOSHIFT[0] to bring the field of interest to the least-significant
    8445              :    bit.  The expectation is that the same loaded word will be
    8446              :    propagated from part 0 to part 1, with just different shifting and
    8447              :    masking to extract both parts.  MASK is not expected to do more
    8448              :    than masking out the bits that belong to the other part.  See
    8449              :    build_split_load for more information on the other fields.  */
    8450              : 
    8451              : static inline void
    8452           51 : reuse_split_load (tree /* in[0] out[1] */ ln_arg[2],
    8453              :                   HOST_WIDE_INT /* in[0] out[1] */ bitpos[2],
    8454              :                   HOST_WIDE_INT /* in[0] out[1] */ bitsiz[2],
    8455              :                   HOST_WIDE_INT /* in[0] out[0..1] */ toshift[2],
    8456              :                   HOST_WIDE_INT /* out */ shifted[2],
    8457              :                   wide_int /* out */ mask[2],
    8458              :                   HOST_WIDE_INT boundary, bool reversep)
    8459              : {
    8460           51 :   unsigned prec = TYPE_PRECISION (TREE_TYPE (ln_arg[0]));
    8461              : 
    8462           51 :   ln_arg[1] = ln_arg[0];
    8463           51 :   bitpos[1] = bitpos[0];
    8464           51 :   bitsiz[1] = bitsiz[0];
    8465           51 :   shifted[1] = shifted[0] = 0;
    8466              : 
    8467           51 :   if (reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
    8468              :     {
    8469            3 :       toshift[1] = toshift[0];
    8470            3 :       toshift[0] = bitpos[0] + bitsiz[0] - boundary;
    8471            3 :       mask[0] = wi::mask (toshift[0], true, prec);
    8472            3 :       mask[1] = wi::mask (toshift[0], false, prec);
    8473              :     }
    8474              :   else
    8475              :     {
    8476           48 :       toshift[1] = boundary - bitpos[1];
    8477           48 :       mask[1] = wi::mask (toshift[1], true, prec);
    8478           48 :       mask[0] = wi::mask (toshift[1], false, prec);
    8479              :     }
    8480           51 : }
    8481              : 
    8482              : /* Find ways of folding logical expressions of LHS and RHS:
    8483              : 
    8484              :    Try to merge two comparisons to nearby fields.
    8485              : 
    8486              :    For example, if we have p->a == 2 && p->b == 4 and we can load both A and B
    8487              :    at once, we can do this with a comparison against the object ANDed with the
    8488              :    a mask.
    8489              : 
    8490              :    If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
    8491              :    operations to do this with one comparison, loading both fields from P at
    8492              :    once, and likewise from Q.
    8493              : 
    8494              :    Herein, loading at once means loading from within the same alignment
    8495              :    boundary for the enclosing object.  If (packed) fields cross such alignment
    8496              :    boundaries, we may still recombine the compares, so that loads do not cross
    8497              :    the boundaries.
    8498              : 
    8499              :    CODE is the logical operation being done.  It can be TRUTH_ANDIF_EXPR,
    8500              :    TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
    8501              : 
    8502              :    TRUTH_TYPE is the type of the logical operand.
    8503              : 
    8504              :    LHS is denoted as LL_ARG LCODE LR_ARG.
    8505              : 
    8506              :    RHS is denoted as RL_ARG RCODE RR_ARG.
    8507              : 
    8508              :    LHS is assumed to dominate RHS.
    8509              : 
    8510              :    Combined loads are inserted next to preexisting loads, once we determine
    8511              :    that the combination is viable, and the combined condition references new
    8512              :    SSA_NAMEs that hold the loaded values.  Since the original loads are
    8513              :    verified to have the same gimple_vuse, the insertion point doesn't matter
    8514              :    for correctness.  ??? The loads may be a lot earlier than the compares, and
    8515              :    it's conceivable that one or two loads for RHS appear before those for LHS.
    8516              :    It could be advantageous to try to place the loads optimally, taking
    8517              :    advantage of knowing whether RHS is accessed before LHS, or that both are
    8518              :    accessed before both compares, but we don't do that (yet?).
    8519              : 
    8520              :    SEPARATEP should be NULL if the combined condition must be returned as a
    8521              :    single expression, even if it is a compound condition.  This must only be
    8522              :    done if LHS and RHS are adjacent, without intervening conditions, and the
    8523              :    combined condition is to replace RHS, while LHS is dropped altogether.
    8524              : 
    8525              :    Otherwise, SEPARATEP must be a non-NULL pointer to a NULL_TREE, that may be
    8526              :    replaced by a part of the compound condition that could replace RHS, while
    8527              :    the returned expression replaces LHS.  This works whether or not LHS and RHS
    8528              :    are adjacent, as long as there aren't VDEFs or other side effects between
    8529              :    them.
    8530              : 
    8531              :    If the "words" accessed by RHS are already accessed by LHS, this won't
    8532              :    matter, but if RHS accesses "words" that LHS doesn't, then *SEPARATEP will
    8533              :    be set to the compares that should take RHS's place.  By "words" we mean
    8534              :    contiguous bits that do not cross a an TYPE_ALIGN boundary of the accessed
    8535              :    object's type.
    8536              : 
    8537              :    We return the simplified tree or 0 if no optimization is possible.  */
    8538              : 
    8539              : tree
    8540       442844 : fold_truth_andor_for_ifcombine (enum tree_code code, tree truth_type,
    8541              :                                 location_t lloc, enum tree_code lcode,
    8542              :                                 tree ll_arg, tree lr_arg,
    8543              :                                 location_t rloc, enum tree_code rcode,
    8544              :                                 tree rl_arg, tree rr_arg,
    8545              :                                 tree *separatep)
    8546              : {
    8547              :   /* If this is the "or" of two comparisons, we can do something if
    8548              :      the comparisons are NE_EXPR.  If this is the "and", we can do something
    8549              :      if the comparisons are EQ_EXPR.  I.e.,
    8550              :         (a->b == 2 && a->c == 4) can become (a->new == NEW).
    8551              : 
    8552              :      WANTED_CODE is this operation code.  For single bit fields, we can
    8553              :      convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
    8554              :      comparison for one-bit fields.  */
    8555              : 
    8556       442844 :   enum tree_code orig_code = code;
    8557       442844 :   enum tree_code wanted_code;
    8558       442844 :   tree ll_inner, lr_inner, rl_inner, rr_inner;
    8559       442844 :   gimple *ll_load, *lr_load, *rl_load, *rr_load;
    8560       442844 :   HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
    8561       442844 :   HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
    8562       442844 :   HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
    8563       442844 :   HOST_WIDE_INT lnbitsize, lnbitpos, lnprec;
    8564       442844 :   HOST_WIDE_INT rnbitsize, rnbitpos, rnprec;
    8565       442844 :   bool ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
    8566       442844 :   bool ll_reversep, lr_reversep, rl_reversep, rr_reversep;
    8567       442844 :   int ll_signbit, lr_signbit, rl_signbit, rr_signbit;
    8568       442844 :   scalar_int_mode lnmode, lnmode2, rnmode;
    8569       442844 :   wide_int ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
    8570       442844 :   wide_int l_const, r_const;
    8571       442844 :   tree lntype, rntype, result;
    8572       442844 :   HOST_WIDE_INT first_bit, end_bit;
    8573       442844 :   bool volatilep;
    8574       442844 :   bool l_split_load;
    8575              : 
    8576              :   /* These are indexed by: conv, mask, shft, load.  */
    8577       442844 :   location_t ll_loc[4] = { lloc, lloc, lloc, UNKNOWN_LOCATION };
    8578       442844 :   location_t lr_loc[4] = { lloc, lloc, lloc, UNKNOWN_LOCATION };
    8579       442844 :   location_t rl_loc[4] = { rloc, rloc, rloc, UNKNOWN_LOCATION };
    8580       442844 :   location_t rr_loc[4] = { rloc, rloc, rloc, UNKNOWN_LOCATION };
    8581              : 
    8582       442844 :   gcc_checking_assert (!separatep || !*separatep);
    8583              : 
    8584              :   /* Start by getting the comparison codes.  Fail if anything is volatile.
    8585              :      If one operand is a BIT_AND_EXPR with the constant one, treat it as if
    8586              :      it were surrounded with a NE_EXPR.  */
    8587              : 
    8588       442844 :   if (TREE_CODE_CLASS (lcode) != tcc_comparison
    8589       442844 :       || TREE_CODE_CLASS (rcode) != tcc_comparison)
    8590              :     return 0;
    8591              : 
    8592              :   /* We don't normally find TRUTH_*IF_EXPR in gimple, but these codes may be
    8593              :      given by our caller to denote conditions from different blocks.  */
    8594       442844 :   switch (code)
    8595              :     {
    8596              :     case TRUTH_AND_EXPR:
    8597              :     case TRUTH_ANDIF_EXPR:
    8598              :       code = TRUTH_AND_EXPR;
    8599              :       break;
    8600              : 
    8601            0 :     case TRUTH_OR_EXPR:
    8602            0 :     case TRUTH_ORIF_EXPR:
    8603            0 :       code = TRUTH_OR_EXPR;
    8604            0 :       break;
    8605              : 
    8606              :     default:
    8607              :       return 0;
    8608              :     }
    8609              : 
    8610              :   /* Prepare to turn compares of signed quantities with zero into sign-bit
    8611              :      tests.  We need not worry about *_reversep here for these compare
    8612              :      rewrites: loads will have already been reversed before compares.  Save the
    8613              :      precision, because [lr]l_arg may change and we won't be able to tell how
    8614              :      wide it was originally.  */
    8615       442844 :   unsigned lsignbit = 0, rsignbit = 0;
    8616       442844 :   if ((lcode == LT_EXPR || lcode == GE_EXPR)
    8617        15080 :       && integer_zerop (lr_arg)
    8618         3769 :       && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg))
    8619       446613 :       && !TYPE_UNSIGNED (TREE_TYPE (ll_arg)))
    8620              :     {
    8621         3769 :       lsignbit = TYPE_PRECISION (TREE_TYPE (ll_arg));
    8622         3769 :       lcode = (lcode == LT_EXPR ? NE_EXPR : EQ_EXPR);
    8623              :     }
    8624              :   /* Turn compares of unsigned quantities with powers of two into
    8625              :      equality tests of masks.  */
    8626       439075 :   else if ((lcode == LT_EXPR || lcode == GE_EXPR)
    8627        11311 :            && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg))
    8628        10545 :            && TYPE_UNSIGNED (TREE_TYPE (ll_arg))
    8629         8259 :            && TREE_CODE (lr_arg) == INTEGER_CST
    8630       439075 :            && wi::popcount (wi::to_wide (lr_arg)) == 1)
    8631              :     {
    8632            0 :       ll_and_mask = ~(wi::to_wide (lr_arg) - 1);
    8633            0 :       lcode = (lcode == GE_EXPR ? NE_EXPR : EQ_EXPR);
    8634            0 :       lr_arg = wide_int_to_tree (TREE_TYPE (ll_arg), ll_and_mask * 0);
    8635              :     }
    8636              :   /* Turn compares of unsigned quantities with powers of two minus one
    8637              :      into equality tests of masks.  */
    8638       878150 :   else if ((lcode == LE_EXPR || lcode == GT_EXPR)
    8639        35118 :            && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg))
    8640        34888 :            && TYPE_UNSIGNED (TREE_TYPE (ll_arg))
    8641        28357 :            && TREE_CODE (lr_arg) == INTEGER_CST
    8642       499520 :            && wi::popcount (wi::to_wide (lr_arg) + 1) == 1)
    8643              :     {
    8644         4236 :       ll_and_mask = ~wi::to_wide (lr_arg);
    8645         4236 :       lcode = (lcode == GT_EXPR ? NE_EXPR : EQ_EXPR);
    8646         4236 :       lr_arg = wide_int_to_tree (TREE_TYPE (ll_arg), ll_and_mask * 0);
    8647              :     }
    8648              :   /* Likewise for the second compare.  */
    8649       442844 :   if ((rcode == LT_EXPR || rcode == GE_EXPR)
    8650        16570 :       && integer_zerop (rr_arg)
    8651         2765 :       && INTEGRAL_TYPE_P (TREE_TYPE (rl_arg))
    8652       445609 :       && !TYPE_UNSIGNED (TREE_TYPE (rl_arg)))
    8653              :     {
    8654         2765 :       rsignbit = TYPE_PRECISION (TREE_TYPE (rl_arg));
    8655         2765 :       rcode = (rcode == LT_EXPR ? NE_EXPR : EQ_EXPR);
    8656              :     }
    8657       440079 :   else if ((rcode == LT_EXPR || rcode == GE_EXPR)
    8658        13805 :            && INTEGRAL_TYPE_P (TREE_TYPE (rl_arg))
    8659        12674 :            && TYPE_UNSIGNED (TREE_TYPE (rl_arg))
    8660         4635 :            && TREE_CODE (rr_arg) == INTEGER_CST
    8661       440079 :            && wi::popcount (wi::to_wide (rr_arg)) == 1)
    8662              :     {
    8663            0 :       rl_and_mask = ~(wi::to_wide (rr_arg) - 1);
    8664            0 :       rcode = (rcode == GE_EXPR ? NE_EXPR : EQ_EXPR);
    8665            0 :       rr_arg = wide_int_to_tree (TREE_TYPE (rl_arg), rl_and_mask * 0);
    8666              :     }
    8667       880158 :   else if ((rcode == LE_EXPR || rcode == GT_EXPR)
    8668        42391 :            && INTEGRAL_TYPE_P (TREE_TYPE (rl_arg))
    8669        42148 :            && TYPE_UNSIGNED (TREE_TYPE (rl_arg))
    8670        32682 :            && TREE_CODE (rr_arg) == INTEGER_CST
    8671       505423 :            && wi::popcount (wi::to_wide (rr_arg) + 1) == 1)
    8672              :     {
    8673         3120 :       rl_and_mask = ~wi::to_wide (rr_arg);
    8674         3120 :       rcode = (rcode == GT_EXPR ? NE_EXPR : EQ_EXPR);
    8675         3120 :       rr_arg = wide_int_to_tree (TREE_TYPE (rl_arg), rl_and_mask * 0);
    8676              :     }
    8677              : 
    8678              :   /* See if the comparisons can be merged.  Then get all the parameters for
    8679              :      each side.  */
    8680              : 
    8681       442844 :   if ((lcode != EQ_EXPR && lcode != NE_EXPR)
    8682       399503 :       || (rcode != EQ_EXPR && rcode != NE_EXPR))
    8683              :     return 0;
    8684              : 
    8685       379035 :   ll_reversep = lr_reversep = rl_reversep = rr_reversep = 0;
    8686       379035 :   volatilep = 0;
    8687       379035 :   bool l_xor = false, r_xor = false;
    8688       379035 :   ll_inner = decode_field_reference (&ll_arg, &ll_bitsize, &ll_bitpos,
    8689              :                                      &ll_unsignedp, &ll_reversep, &volatilep,
    8690              :                                      &ll_and_mask, &ll_signbit,
    8691              :                                      &l_xor, &lr_arg, &lr_and_mask,
    8692              :                                      &ll_load, ll_loc);
    8693       379035 :   if (!ll_inner)
    8694              :     return 0;
    8695       284485 :   lr_inner = decode_field_reference (&lr_arg, &lr_bitsize, &lr_bitpos,
    8696              :                                      &lr_unsignedp, &lr_reversep, &volatilep,
    8697              :                                      &lr_and_mask, &lr_signbit, &l_xor, 0, 0,
    8698              :                                      &lr_load, lr_loc);
    8699       284485 :   if (!lr_inner)
    8700              :     return 0;
    8701       279259 :   rl_inner = decode_field_reference (&rl_arg, &rl_bitsize, &rl_bitpos,
    8702              :                                      &rl_unsignedp, &rl_reversep, &volatilep,
    8703              :                                      &rl_and_mask, &rl_signbit,
    8704              :                                      &r_xor, &rr_arg, &rr_and_mask,
    8705              :                                      &rl_load, rl_loc);
    8706       279259 :   if (!rl_inner)
    8707              :     return 0;
    8708       259051 :   rr_inner = decode_field_reference (&rr_arg, &rr_bitsize, &rr_bitpos,
    8709              :                                      &rr_unsignedp, &rr_reversep, &volatilep,
    8710              :                                      &rr_and_mask, &rr_signbit, &r_xor, 0, 0,
    8711              :                                      &rr_load, rr_loc);
    8712       259051 :   if (!rr_inner)
    8713              :     return 0;
    8714              : 
    8715              :   /* It must be true that the inner operation on the lhs of each
    8716              :      comparison must be the same if we are to be able to do anything.
    8717              :      Then see if we have constants.  If not, the same must be true for
    8718              :      the rhs's.  If one is a load and the other isn't, we have to be
    8719              :      conservative and avoid the optimization, otherwise we could get
    8720              :      SRAed fields wrong.  */
    8721       256452 :   if (volatilep)
    8722              :     return 0;
    8723              : 
    8724       256443 :   if (ll_reversep != rl_reversep
    8725       256443 :       || ! operand_equal_p (ll_inner, rl_inner, 0))
    8726              :     {
    8727              :       /* Try swapping the operands.  */
    8728       196221 :       if (ll_reversep != rr_reversep || rsignbit
    8729       391669 :           || !operand_equal_p (ll_inner, rr_inner, 0))
    8730              :         return 0;
    8731              : 
    8732         1654 :       rcode = swap_tree_comparison (rcode);
    8733         1654 :       std::swap (rl_arg, rr_arg);
    8734         1654 :       std::swap (rl_inner, rr_inner);
    8735         1654 :       std::swap (rl_bitsize, rr_bitsize);
    8736         1654 :       std::swap (rl_bitpos, rr_bitpos);
    8737         1654 :       std::swap (rl_unsignedp, rr_unsignedp);
    8738         1654 :       std::swap (rl_reversep, rr_reversep);
    8739         1654 :       std::swap (rl_and_mask, rr_and_mask);
    8740         1654 :       std::swap (rl_signbit, rr_signbit);
    8741         1654 :       std::swap (rl_load, rr_load);
    8742         1654 :       std::swap (rl_loc, rr_loc);
    8743              :     }
    8744              : 
    8745       121360 :   if ((ll_load && rl_load)
    8746       240530 :       ? gimple_vuse (ll_load) != gimple_vuse (rl_load)
    8747         2190 :       : (!ll_load != !rl_load))
    8748              :     return 0;
    8749              : 
    8750              :   /* ??? Can we do anything with these?  */
    8751        60787 :   if (lr_signbit > 0 || rr_signbit > 0)
    8752              :     return 0;
    8753              : 
    8754              :   /* If the mask encompassed extensions of the sign bit before
    8755              :      clipping, try to include the sign bit in the test.  If we're not
    8756              :      comparing with zero, don't even try to deal with it (for now?).
    8757              :      If we've already committed to a sign test, the extended (before
    8758              :      clipping) mask could already be messing with it.  */
    8759        60787 :   if (ll_signbit > 0)
    8760              :     {
    8761           28 :       if (!integer_zerop (lr_arg) || lsignbit)
    8762            0 :         return 0;
    8763           28 :       wide_int sign = wi::mask (ll_bitsize - 1, true, ll_bitsize);
    8764           28 :       if (!ll_and_mask.get_precision ())
    8765            0 :         ll_and_mask = sign;
    8766              :       else
    8767           28 :         ll_and_mask |= sign;
    8768           28 :     }
    8769              : 
    8770        60787 :   if (rl_signbit > 0)
    8771              :     {
    8772            4 :       if (!integer_zerop (rr_arg) || rsignbit)
    8773            1 :         return 0;
    8774            3 :       wide_int sign = wi::mask (rl_bitsize - 1, true, rl_bitsize);
    8775            3 :       if (!rl_and_mask.get_precision ())
    8776            0 :         rl_and_mask = sign;
    8777              :       else
    8778            3 :         rl_and_mask |= sign;
    8779            3 :     }
    8780              : 
    8781        60786 :   if (TREE_CODE (lr_arg) == INTEGER_CST
    8782        47634 :       && TREE_CODE (rr_arg) == INTEGER_CST)
    8783              :     {
    8784        47006 :       l_const = wi::to_wide (lr_arg);
    8785              :       /* We don't expect masks on constants, but if there are any, apply
    8786              :          them now.  */
    8787        47006 :       if (lr_and_mask.get_precision ())
    8788            4 :         l_const &= wide_int::from (lr_and_mask,
    8789            2 :                                    l_const.get_precision (), UNSIGNED);
    8790        47006 :       r_const = wi::to_wide (rr_arg);
    8791        47006 :       if (rr_and_mask.get_precision ())
    8792            0 :         r_const &= wide_int::from (rr_and_mask,
    8793            0 :                                    r_const.get_precision (), UNSIGNED);
    8794        47006 :       lr_reversep = ll_reversep;
    8795              :     }
    8796        13780 :   else if (lr_reversep != rr_reversep
    8797        13772 :            || ! operand_equal_p (lr_inner, rr_inner, 0)
    8798        23998 :            || ((lr_load && rr_load)
    8799        30456 :                ? gimple_vuse (lr_load) != gimple_vuse (rr_load)
    8800           66 :                : (!lr_load != !rr_load)))
    8801              :     return 0;
    8802              : 
    8803              :   /* If we found sign tests, finish turning them into bit tests.  */
    8804              : 
    8805        56912 :   if (lsignbit)
    8806              :     {
    8807           70 :       wide_int sign = wi::mask (ll_bitsize - 1, true, ll_bitsize);
    8808              :       /* If ll_arg is zero-extended and we're testing the sign bit, we know
    8809              :          what the result should be.  Shifting the sign bit out of sign will get
    8810              :          us to mask the entire field out, yielding zero, i.e., the sign bit of
    8811              :          the zero-extended value.  We know the masked value is being compared
    8812              :          with zero, so the compare will get us the result we're looking
    8813              :          for: TRUE if EQ_EXPR, FALSE if NE_EXPR.  Likewise if ll_arg is
    8814              :          masked so that all the extension bits are clear (ll_signbit < 0):
    8815              :          it can't be negative then, even if the field is signed.  */
    8816           70 :       if (lsignbit > ll_bitsize && (ll_unsignedp || ll_signbit < 0))
    8817            5 :         sign <<= 1;
    8818           70 :       if (!ll_and_mask.get_precision ())
    8819           66 :         ll_and_mask = sign;
    8820              :       else
    8821            4 :         ll_and_mask &= sign;
    8822           70 :       if (l_xor)
    8823              :         {
    8824            3 :           if (ll_bitsize != lr_bitsize)
    8825            1 :             return 0;
    8826            2 :           if (!lr_and_mask.get_precision ())
    8827            2 :             lr_and_mask = sign;
    8828              :           else
    8829            0 :             lr_and_mask &= sign;
    8830            2 :           if (l_const.get_precision ())
    8831            0 :             l_const &= wide_int::from (lr_and_mask,
    8832            0 :                                        l_const.get_precision (), UNSIGNED);
    8833              :         }
    8834           70 :     }
    8835              : 
    8836        56911 :   if (rsignbit)
    8837              :     {
    8838          198 :       wide_int sign = wi::mask (rl_bitsize - 1, true, rl_bitsize);
    8839          198 :       if (rsignbit > rl_bitsize && (rl_unsignedp || rl_signbit < 0))
    8840            1 :         sign <<= 1;
    8841          198 :       if (!rl_and_mask.get_precision ())
    8842          182 :         rl_and_mask = sign;
    8843              :       else
    8844           16 :         rl_and_mask &= sign;
    8845          198 :       if (r_xor)
    8846              :         {
    8847            5 :           if (rl_bitsize != rr_bitsize)
    8848            0 :             return 0;
    8849            5 :           if (!rr_and_mask.get_precision ())
    8850            5 :             rr_and_mask = sign;
    8851              :           else
    8852            0 :             rr_and_mask &= sign;
    8853            5 :           if (r_const.get_precision ())
    8854            0 :             r_const &= wide_int::from (rr_and_mask,
    8855            0 :                                        r_const.get_precision (), UNSIGNED);
    8856              :         }
    8857          198 :     }
    8858              : 
    8859              :   /* If either comparison code is not correct for our logical operation,
    8860              :      fail.  However, we can convert a one-bit comparison against zero into
    8861              :      the opposite comparison against that bit being set in the field.  */
    8862              : 
    8863        56911 :   wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
    8864        56911 :   if (lcode != wanted_code)
    8865              :     {
    8866         5064 :       if (l_const.get_precision ()
    8867         4892 :           && l_const == 0
    8868         1660 :           && ll_and_mask.get_precision ()
    8869         5452 :           && wi::popcount (ll_and_mask) == 1)
    8870              :         {
    8871              :           /* Make the left operand unsigned, since we are only interested
    8872              :              in the value of one bit.  Otherwise we are doing the wrong
    8873              :              thing below.  */
    8874          228 :           ll_unsignedp = 1;
    8875          228 :           l_const = ll_and_mask;
    8876              :         }
    8877              :       else
    8878         4836 :         return 0;
    8879              :     }
    8880              : 
    8881              :   /* This is analogous to the code for l_const above.  */
    8882        52075 :   if (rcode != wanted_code)
    8883              :     {
    8884         1010 :       if (r_const.get_precision ()
    8885         1010 :           && r_const == 0
    8886          963 :           && rl_and_mask.get_precision ()
    8887         1903 :           && wi::popcount (rl_and_mask) == 1)
    8888              :         {
    8889          548 :           rl_unsignedp = 1;
    8890          548 :           r_const = rl_and_mask;
    8891              :         }
    8892              :       else
    8893          462 :         return 0;
    8894              :     }
    8895              : 
    8896              :   /* This will be bumped to 2 if any of the field pairs crosses an
    8897              :      alignment boundary, so the merged compare has to be done in two
    8898              :      parts.  */
    8899        51613 :   int parts = 1;
    8900              :   /* Set to true if the second combined compare should come first,
    8901              :      e.g., because the second original compare accesses a word that
    8902              :      the first one doesn't, and the combined compares access those in
    8903              :      cmp[0].  */
    8904        51613 :   bool first1 = false;
    8905              :   /* Set to true if the first original compare is not the one being
    8906              :      split.  */
    8907        51613 :   bool maybe_separate = false;
    8908              : 
    8909              :   /* The following 2-dimensional arrays use the first index to
    8910              :      identify left(0)- vs right(1)-hand compare operands, and the
    8911              :      second one to identify merged compare parts.  */
    8912              :   /* The memory loads or constants to be compared.  */
    8913        51613 :   tree ld_arg[2][2];
    8914              :   /* The first bit of the corresponding inner object that the
    8915              :      corresponding LD_ARG covers.  */
    8916        51613 :   HOST_WIDE_INT bitpos[2][2];
    8917              :   /* The bit count starting at BITPOS that the corresponding LD_ARG
    8918              :      covers.  */
    8919        51613 :   HOST_WIDE_INT bitsiz[2][2];
    8920              :   /* The number of bits by which LD_ARG has already been shifted
    8921              :      right, WRT mask.  */
    8922        51613 :   HOST_WIDE_INT shifted[2][2];
    8923              :   /* The number of bits by which both LD_ARG and MASK need shifting to
    8924              :      bring its least-significant bit to bit zero.  */
    8925        51613 :   HOST_WIDE_INT toshift[2][2];
    8926              :   /* An additional mask to be applied to LD_ARG, to remove any bits
    8927              :      that may have been loaded for use in another compare, but that
    8928              :      don't belong in the corresponding compare.  */
    8929       619356 :   wide_int xmask[2][2] = {};
    8930              : 
    8931              :   /* The combined compare or compares.  */
    8932        51613 :   tree cmp[2];
    8933              : 
    8934              :   /* Consider we're comparing two non-contiguous fields of packed
    8935              :      structs, both aligned at 32-bit boundaries:
    8936              : 
    8937              :      ll_arg: an 8-bit field at offset 0
    8938              :      lr_arg: a 16-bit field at offset 2
    8939              : 
    8940              :      rl_arg: an 8-bit field at offset 1
    8941              :      rr_arg: a 16-bit field at offset 3
    8942              : 
    8943              :      We'll have r_split_load, because rr_arg straddles across an
    8944              :      alignment boundary.
    8945              : 
    8946              :      We'll want to have:
    8947              : 
    8948              :      bitpos  = { {  0,  0 }, {  0, 32 } }
    8949              :      bitsiz  = { { 32, 32 }, { 32,  8 } }
    8950              : 
    8951              :      And, for little-endian:
    8952              : 
    8953              :      shifted = { {  0,  0 }, {  0, 32 } }
    8954              :      toshift = { {  0, 24 }, {  0,  0 } }
    8955              : 
    8956              :      Or, for big-endian:
    8957              : 
    8958              :      shifted = { {  0,  0 }, {  8,  0 } }
    8959              :      toshift = { {  8,  0 }, {  0,  0 } }
    8960              :   */
    8961              : 
    8962              :   /* See if we can find a mode that contains both fields being compared on
    8963              :      the left.  If we can't, fail.  Otherwise, update all constants and masks
    8964              :      to be relative to a field of that size.  */
    8965        51613 :   first_bit = MIN (ll_bitpos, rl_bitpos);
    8966        51613 :   end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
    8967        51613 :   HOST_WIDE_INT ll_align = TYPE_ALIGN (TREE_TYPE (ll_inner));
    8968        51613 :   poly_uint64 ll_end_region = 0;
    8969        51613 :   if (TYPE_SIZE (TREE_TYPE (ll_inner))
    8970        51613 :       && tree_fits_poly_uint64_p (TYPE_SIZE (TREE_TYPE (ll_inner))))
    8971        51613 :     ll_end_region = tree_to_poly_uint64 (TYPE_SIZE (TREE_TYPE (ll_inner)));
    8972        51613 :   if (get_best_mode (end_bit - first_bit, first_bit, 0, ll_end_region,
    8973        51613 :                      ll_align, BITS_PER_WORD, volatilep, &lnmode))
    8974              :     l_split_load = false;
    8975              :   /* ??? If ll and rl share the same load, reuse that?
    8976              :      See PR 118206 -> gcc.dg/field-merge-18.c  */
    8977              :   else
    8978              :     {
    8979              :       /* Consider the possibility of recombining loads if any of the
    8980              :          fields straddles across an alignment boundary, so that either
    8981              :          part can be loaded along with the other field.  Since we
    8982              :          limit access modes to BITS_PER_WORD, don't exceed that,
    8983              :          otherwise on a 32-bit host and a 64-bit-aligned data
    8984              :          structure, we'll fail the above for a field that straddles
    8985              :          across two words, and would fail here for not even trying to
    8986              :          split it at between 32-bit words.  */
    8987        48175 :       HOST_WIDE_INT boundary = compute_split_boundary_from_align
    8988        51397 :         (MIN (ll_align, BITS_PER_WORD),
    8989              :          ll_bitpos, ll_bitsize, rl_bitpos, rl_bitsize);
    8990              : 
    8991        48175 :       if (boundary < 0
    8992          219 :           || !get_best_mode (boundary - first_bit, first_bit, 0, ll_end_region,
    8993              :                              ll_align, BITS_PER_WORD, volatilep, &lnmode)
    8994        48352 :           || !get_best_mode (end_bit - boundary, boundary, 0, ll_end_region,
    8995          177 :                              ll_align, BITS_PER_WORD, volatilep, &lnmode2))
    8996              :         {
    8997        51178 :           if (ll_align <= BITS_PER_WORD)
    8998              :             return 0;
    8999              : 
    9000              :           /* As a last resort, try double-word access modes.  This
    9001              :              enables us to deal with misaligned double-word fields
    9002              :              that straddle across 3 separate words.  */
    9003         1998 :           boundary = compute_split_boundary_from_align
    9004         2100 :             (MIN (ll_align, 2 * BITS_PER_WORD),
    9005              :              ll_bitpos, ll_bitsize, rl_bitpos, rl_bitsize);
    9006         1998 :           if (boundary < 0
    9007            0 :               || !get_best_mode (boundary - first_bit, first_bit,
    9008              :                                  0, ll_end_region, ll_align, 2 * BITS_PER_WORD,
    9009              :                                  volatilep, &lnmode)
    9010         1998 :               || !get_best_mode (end_bit - boundary, boundary,
    9011            0 :                                  0, ll_end_region, ll_align, 2 * BITS_PER_WORD,
    9012              :                                  volatilep, &lnmode2))
    9013              :             return 0;
    9014              :         }
    9015              : 
    9016              :       /* If we can't have a single load, but can with two, figure out whether
    9017              :          the two compares can be separated, i.e., whether the entirety of the
    9018              :          first original compare is encompassed by the entirety of the first
    9019              :          combined compare.  If the first original compare is past the alignment
    9020              :          boundary, arrange to compare that range first, by setting first1
    9021              :          (meaning make cmp[1] first, instead of cmp[0]).  */
    9022          177 :       l_split_load = true;
    9023          177 :       parts = 2;
    9024          177 :       if (ll_bitpos >= boundary)
    9025              :         maybe_separate = first1 = true;
    9026          132 :       else if (ll_bitpos + ll_bitsize <= boundary)
    9027           32 :         maybe_separate = true;
    9028              :     }
    9029              : 
    9030         3615 :   lnbitsize = GET_MODE_BITSIZE (lnmode);
    9031         3615 :   lnbitpos = first_bit & ~ (lnbitsize - 1);
    9032              :   /* Avoid situations that the code below can't handle.  */
    9033         3615 :   if (lnbitpos < 0)
    9034              :     return 0;
    9035              : 
    9036              :   /* Choose the type for the combined compare.  Even if we're splitting loads,
    9037              :      make it wide enough to hold both.  */
    9038         3615 :   if (l_split_load)
    9039          354 :     lnbitsize += GET_MODE_BITSIZE (lnmode2);
    9040         3615 :   lntype = build_nonstandard_integer_type (lnbitsize, 1);
    9041         3615 :   if (!lntype)
    9042              :     return NULL_TREE;
    9043         3615 :   lnprec = TYPE_PRECISION (lntype);
    9044         3615 :   xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
    9045              : 
    9046              :   /* Adjust bit ranges for reverse endianness.  */
    9047         3615 :   if (ll_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
    9048              :     {
    9049            6 :       xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
    9050            6 :       xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
    9051              :     }
    9052              : 
    9053              :   /* Adjust masks to match the positions in the combined lntype.  */
    9054         7230 :   wide_int ll_mask, rl_mask, r_mask;
    9055         3615 :   if (ll_and_mask.get_precision ())
    9056         4446 :     ll_mask = wi::lshift (wide_int::from (ll_and_mask, lnprec, UNSIGNED),
    9057         2223 :                           xll_bitpos);
    9058              :   else
    9059         1392 :     ll_mask = wi::shifted_mask (xll_bitpos, ll_bitsize, false, lnprec);
    9060         3615 :   if (rl_and_mask.get_precision ())
    9061         4076 :     rl_mask = wi::lshift (wide_int::from (rl_and_mask, lnprec, UNSIGNED),
    9062         2038 :                           xrl_bitpos);
    9063              :   else
    9064         1577 :     rl_mask = wi::shifted_mask (xrl_bitpos, rl_bitsize, false, lnprec);
    9065              : 
    9066              :   /* When we set l_const, we also set r_const.  */
    9067         3615 :   gcc_checking_assert (!l_const.get_precision () == !r_const.get_precision ());
    9068              : 
    9069              :   /* Adjust right-hand constants in both original comparisons to match width
    9070              :      and bit position.  */
    9071         3615 :   if (l_const.get_precision ())
    9072              :     {
    9073              :       /* Before clipping upper bits of the right-hand operand of the compare,
    9074              :          check that they're sign or zero extensions, depending on how the
    9075              :          left-hand operand would be extended.  If it is unsigned, or if there's
    9076              :          a mask that zeroes out extension bits, whether because we've checked
    9077              :          for upper bits in the mask and found them all clear (ll_signbit < 0),
    9078              :          or because the sign bit itself is masked out, check that the
    9079              :          right-hand operand is zero-extended.  */
    9080         2209 :       bool l_non_ext_bits = false;
    9081         2209 :       if (ll_bitsize < lr_bitsize)
    9082              :         {
    9083           81 :           wide_int zext = wi::zext (l_const, ll_bitsize);
    9084          162 :           if ((ll_unsignedp
    9085           69 :                || ll_signbit < 0
    9086           57 :                || (ll_and_mask.get_precision ()
    9087          139 :                    && ((ll_and_mask & wi::mask (ll_bitsize - 1, true, ll_bitsize))
    9088           58 :                        == 0))
    9089          312 :                ? zext : wi::sext (l_const, ll_bitsize)) == l_const)
    9090           80 :             l_const = zext;
    9091              :           else
    9092              :             l_non_ext_bits = true;
    9093           81 :         }
    9094              :       /* We're doing bitwise equality tests, so don't bother with sign
    9095              :          extensions.  */
    9096         2209 :       l_const = wide_int::from (l_const, lnprec, UNSIGNED);
    9097         2209 :       if (ll_and_mask.get_precision ())
    9098         1220 :         l_const &= wide_int::from (ll_and_mask, lnprec, UNSIGNED);
    9099         2209 :       l_const <<= xll_bitpos;
    9100         6625 :       if (l_non_ext_bits || (l_const & ~ll_mask) != 0)
    9101              :         {
    9102            1 :           warning_at (lloc, OPT_Wtautological_compare,
    9103              :                       "comparison is always %d", wanted_code == NE_EXPR);
    9104              : 
    9105            1 :           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
    9106              :         }
    9107              : 
    9108              :       /* Before clipping upper bits of the right-hand operand of the compare,
    9109              :          check that they're sign or zero extensions, depending on how the
    9110              :          left-hand operand would be extended.  */
    9111         2208 :       bool r_non_ext_bits = false;
    9112         2208 :       if (rl_bitsize < rr_bitsize)
    9113              :         {
    9114           11 :           wide_int zext = wi::zext (r_const, rl_bitsize);
    9115           22 :           if ((rl_unsignedp
    9116            7 :                || rl_signbit < 0
    9117            5 :                || (rl_and_mask.get_precision ()
    9118           19 :                    && ((rl_and_mask & wi::mask (rl_bitsize - 1, true, rl_bitsize))
    9119            8 :                        == 0))
    9120           40 :                ? zext : wi::sext (r_const, rl_bitsize)) == r_const)
    9121           11 :             r_const = zext;
    9122              :           else
    9123              :             r_non_ext_bits = true;
    9124           11 :         }
    9125         2208 :       r_const = wide_int::from (r_const, lnprec, UNSIGNED);
    9126         2208 :       if (rl_and_mask.get_precision ())
    9127         1083 :         r_const &= wide_int::from (rl_and_mask, lnprec, UNSIGNED);
    9128         2208 :       r_const <<= xrl_bitpos;
    9129         6624 :       if (r_non_ext_bits || (r_const & ~rl_mask) != 0)
    9130              :         {
    9131            0 :           warning_at (rloc, OPT_Wtautological_compare,
    9132              :                       "comparison is always %d", wanted_code == NE_EXPR);
    9133              : 
    9134            0 :           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
    9135              :         }
    9136              : 
    9137              :       /* If there is something in common between the masks, those bits of the
    9138              :          constants must be the same.  If not, the combined condition cannot be
    9139              :          met, and the result is known.  Test for this to avoid generating
    9140              :          incorrect code below.  */
    9141         2208 :       wide_int mask = ll_mask & rl_mask;
    9142         2208 :       if (mask != 0
    9143         2298 :           && (l_const & mask) != (r_const & mask))
    9144              :         {
    9145            0 :           if (wanted_code == NE_EXPR)
    9146            0 :             return constant_boolean_node (true, truth_type);
    9147              :           else
    9148            0 :             return constant_boolean_node (false, truth_type);
    9149              :         }
    9150              : 
    9151              :       /* The constants are combined so as to line up with the loaded field, so
    9152              :          tentatively use the same parameters for the second combined
    9153              :          compare.  */
    9154         2208 :       ld_arg[1][0] = wide_int_to_tree (lntype, l_const | r_const);
    9155         2208 :       toshift[1][0] = MIN (xll_bitpos, xrl_bitpos);
    9156         2208 :       shifted[1][0] = 0;
    9157         2208 :       bitpos[1][0] = lnbitpos;
    9158         2208 :       bitsiz[1][0] = lnbitsize;
    9159              : 
    9160         2208 :       if (parts > 1)
    9161           49 :         reuse_split_load (ld_arg[1], bitpos[1], bitsiz[1], toshift[1],
    9162              :                           shifted[1], xmask[1],
    9163           49 :                           lnbitpos + GET_MODE_BITSIZE (lnmode),
    9164              :                           lr_reversep);
    9165              : 
    9166              :       /* No masking needed, we know the full constants.  */
    9167         2208 :       r_mask = wi::mask (0, true, lnprec);
    9168              : 
    9169              :       /* If the compiler thinks this is used uninitialized below, it's
    9170              :          because it can't realize that parts can only be 2 when
    9171              :          comparing with constants if l_split_load is also true.  This
    9172              :          just silences the warning.  */
    9173         2208 :       rnbitpos = 0;
    9174         2208 :     }
    9175              : 
    9176              :   /* Likewise, if the right sides are not constant, align them for the combined
    9177              :      compare.  Also, disallow this optimization if a size, signedness or
    9178              :      storage order mismatch occurs between the left and right sides.
    9179              :      A signed field whose mask clears all the extension bits
    9180              :      (signbit < 0) is effectively zero-extended, so it must not be
    9181              :      compared with a sign-extended field: clipping both to the field
    9182              :      width would drop the test that the sign bit of the latter is
    9183              :      clear.  */
    9184              :   else
    9185              :     {
    9186         1406 :       if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
    9187         2640 :           || ((ll_unsignedp || ll_signbit < 0)
    9188         1320 :               != (lr_unsignedp || lr_signbit < 0))
    9189         2634 :           || ((rl_unsignedp || rl_signbit < 0)
    9190         1317 :               != (rr_unsignedp || rr_signbit < 0))
    9191         1316 :           || ll_reversep != lr_reversep
    9192              :           /* Make sure the two fields on the right
    9193              :              correspond to the left without being swapped.  */
    9194         1316 :           || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
    9195          223 :         return 0;
    9196              : 
    9197              :       /* Each compare tests, for every bit in its masks, either that the bits
    9198              :          on both sides are equal (bit in both masks), or that the bit on one
    9199              :          side is zero (bit in one mask only).  The combined compare can only
    9200              :          express one such test per bit, so reject combinations in which both
    9201              :          compares test the same bit in different ways.  Check this in the
    9202              :          left-hand frame, since the right-hand fields are laid out alike.  */
    9203         1196 :       wide_int lr_m, rr_m;
    9204         1196 :       if (lr_and_mask.get_precision ())
    9205         2002 :         lr_m = wi::lshift (wide_int::from (lr_and_mask, lnprec, UNSIGNED),
    9206         1001 :                            xll_bitpos);
    9207              :       else
    9208          195 :         lr_m = wi::shifted_mask (xll_bitpos, lr_bitsize, false, lnprec);
    9209         1196 :       if (rr_and_mask.get_precision ())
    9210         1892 :         rr_m = wi::lshift (wide_int::from (rr_and_mask, lnprec, UNSIGNED),
    9211          946 :                            xrl_bitpos);
    9212              :       else
    9213          250 :         rr_m = wi::shifted_mask (xrl_bitpos, rr_bitsize, false, lnprec);
    9214         2392 :       if (((ll_mask | lr_m) & (rl_mask | rr_m)
    9215         3588 :            & ((ll_mask ^ rl_mask) | (lr_m ^ rr_m))) != 0)
    9216              :         return 0;
    9217              : 
    9218         1191 :       bool r_split_load;
    9219         1191 :       scalar_int_mode rnmode2;
    9220              : 
    9221              :       /* Figure out how to load the bits for the right-hand size of the
    9222              :          combined compare.  As in the left-hand size, we may have to split it,
    9223              :          and then we use two separate compares.  */
    9224         1191 :       first_bit = MIN (lr_bitpos, rr_bitpos);
    9225         1191 :       end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
    9226         1191 :       HOST_WIDE_INT lr_align = TYPE_ALIGN (TREE_TYPE (lr_inner));
    9227         1191 :       poly_uint64 lr_end_region = 0;
    9228         1191 :       if (TYPE_SIZE (TREE_TYPE (lr_inner))
    9229         1191 :           && tree_fits_poly_uint64_p (TYPE_SIZE (TREE_TYPE (lr_inner))))
    9230         1191 :         lr_end_region = tree_to_poly_uint64 (TYPE_SIZE (TREE_TYPE (lr_inner)));
    9231         1191 :       if (!get_best_mode (end_bit - first_bit, first_bit, 0, lr_end_region,
    9232         1191 :                           lr_align, BITS_PER_WORD, volatilep, &rnmode))
    9233              :         {
    9234              :           /* Consider the possibility of recombining loads if any of the
    9235              :              fields straddles across an alignment boundary, so that either
    9236              :              part can be loaded along with the other field.  */
    9237          138 :           HOST_WIDE_INT boundary = compute_split_boundary_from_align
    9238          138 :             (lr_align, lr_bitpos, lr_bitsize, rr_bitpos, rr_bitsize);
    9239              : 
    9240          138 :           if (boundary < 0
    9241              :               /* If we're to split both, make sure the split point is
    9242              :                  the same.  */
    9243          130 :               || (l_split_load
    9244          128 :                   && (boundary - lr_bitpos
    9245          128 :                       != (lnbitpos + GET_MODE_BITSIZE (lnmode)) - ll_bitpos))
    9246          130 :               || !get_best_mode (boundary - first_bit, first_bit,
    9247              :                                  0, lr_end_region,
    9248          130 :                                  lr_align, BITS_PER_WORD, volatilep, &rnmode)
    9249          268 :               || !get_best_mode (end_bit - boundary, boundary, 0, lr_end_region,
    9250          130 :                                  lr_align, BITS_PER_WORD, volatilep, &rnmode2))
    9251              :             return 0;
    9252              : 
    9253          130 :           r_split_load = true;
    9254          130 :           parts = 2;
    9255          130 :           if (lr_bitpos >= boundary)
    9256              :             maybe_separate = first1 = true;
    9257           88 :           else if (lr_bitpos + lr_bitsize <= boundary)
    9258           29 :             maybe_separate = true;
    9259              :         }
    9260              :       else
    9261              :         r_split_load = false;
    9262              : 
    9263              :       /* Find a type that can hold the entire right-hand operand.  */
    9264         1183 :       rnbitsize = GET_MODE_BITSIZE (rnmode);
    9265         1183 :       rnbitpos = first_bit & ~ (rnbitsize - 1);
    9266         1183 :       if (r_split_load)
    9267          260 :         rnbitsize += GET_MODE_BITSIZE (rnmode2);
    9268         1183 :       rntype = build_nonstandard_integer_type (rnbitsize, 1);
    9269         1183 :       if (!rntype)
    9270              :         return  0;
    9271         1183 :       rnprec = TYPE_PRECISION (rntype);
    9272         1183 :       xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
    9273              : 
    9274              :       /* Adjust for reversed endianness.  */
    9275         1183 :       if (lr_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
    9276              :         {
    9277            0 :           xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
    9278            0 :           xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
    9279              :         }
    9280              : 
    9281              :       /* Adjust the masks to match the combined type, and combine them.  */
    9282         1183 :       wide_int lr_mask, rr_mask;
    9283         1183 :       if (lr_and_mask.get_precision ())
    9284         1994 :         lr_mask = wi::lshift (wide_int::from (lr_and_mask, rnprec, UNSIGNED),
    9285          997 :                               xlr_bitpos);
    9286              :       else
    9287          186 :         lr_mask = wi::shifted_mask (xlr_bitpos, lr_bitsize, false, rnprec);
    9288         1183 :       if (rr_and_mask.get_precision ())
    9289         1890 :         rr_mask = wi::lshift (wide_int::from (rr_and_mask, rnprec, UNSIGNED),
    9290          945 :                               xrr_bitpos);
    9291              :       else
    9292          238 :         rr_mask = wi::shifted_mask (xrr_bitpos, rr_bitsize, false, rnprec);
    9293         1183 :       r_mask = lr_mask | rr_mask;
    9294              : 
    9295              :       /* Load the right-hand operand of the combined compare.  */
    9296         1183 :       toshift[1][0] = MIN (xlr_bitpos, xrr_bitpos);
    9297         1183 :       shifted[1][0] = 0;
    9298              : 
    9299         1183 :       if (!r_split_load)
    9300              :         {
    9301         1053 :           bitpos[1][0] = rnbitpos;
    9302         1053 :           bitsiz[1][0] = rnbitsize;
    9303         2106 :           ld_arg[1][0] = make_bit_field_load (ll_loc[3], lr_inner, lr_arg,
    9304         1053 :                                               rntype, rnbitsize, rnbitpos,
    9305              :                                               lr_unsignedp || rr_unsignedp,
    9306              :                                               lr_reversep, lr_load);
    9307              :         }
    9308              : 
    9309              :       /* ... and the second part of the right-hand operand if needed.  */
    9310         1183 :       if (parts > 1)
    9311              :         {
    9312          130 :           if (r_split_load)
    9313              :             {
    9314          130 :               gimple *point[2];
    9315          130 :               point[0] = lr_load;
    9316          130 :               point[1] = rr_load;
    9317          130 :               build_split_load (ld_arg[1], bitpos[1], bitsiz[1], toshift[1],
    9318              :                                 shifted[1], rl_loc[3], lr_inner, lr_arg,
    9319              :                                 rnmode, rnmode2, rnbitpos, lr_reversep, point);
    9320              :             }
    9321              :           else
    9322            0 :             reuse_split_load (ld_arg[1], bitpos[1], bitsiz[1], toshift[1],
    9323              :                               shifted[1], xmask[1],
    9324            0 :                               lnbitpos + GET_MODE_BITSIZE (lnmode)
    9325            0 :                               - ll_bitpos + lr_bitpos, lr_reversep);
    9326              :         }
    9327         1196 :     }
    9328              : 
    9329              :   /* Now issue the loads for the left-hand combined operand/s.  */
    9330         6782 :   wide_int l_mask = ll_mask | rl_mask;
    9331         3391 :   toshift[0][0] = MIN (xll_bitpos, xrl_bitpos);
    9332         3391 :   shifted[0][0] = 0;
    9333              : 
    9334         3391 :   if (!l_split_load)
    9335              :     {
    9336         3214 :       bitpos[0][0] = lnbitpos;
    9337         3214 :       bitsiz[0][0] = lnbitsize;
    9338         6428 :       ld_arg[0][0] = make_bit_field_load (ll_loc[3], ll_inner, ll_arg,
    9339         3214 :                                           lntype, lnbitsize, lnbitpos,
    9340              :                                           ll_unsignedp || rl_unsignedp,
    9341              :                                           ll_reversep, ll_load);
    9342              :     }
    9343              : 
    9344         3391 :   if (parts > 1)
    9345              :     {
    9346          179 :       if (l_split_load)
    9347              :             {
    9348          177 :               gimple *point[2];
    9349          177 :               point[0] = ll_load;
    9350          177 :               point[1] = rl_load;
    9351          177 :               build_split_load (ld_arg[0], bitpos[0], bitsiz[0], toshift[0],
    9352              :                                 shifted[0], rl_loc[3], ll_inner, ll_arg,
    9353              :                                 lnmode, lnmode2, lnbitpos, ll_reversep, point);
    9354              :             }
    9355              :       else
    9356            2 :         reuse_split_load (ld_arg[0], bitpos[0], bitsiz[0], toshift[0],
    9357              :                           shifted[0], xmask[0],
    9358            2 :                           rnbitpos + GET_MODE_BITSIZE (rnmode)
    9359            2 :                           - lr_bitpos + ll_bitpos, ll_reversep);
    9360              :     }
    9361              : 
    9362              :   /* Compute the compares.  */
    9363         6961 :   for (int i = 0; i < parts; i++)
    9364              :     {
    9365         3570 :       tree op[2] = { ld_arg[0][i], ld_arg[1][i] };
    9366        10710 :       wide_int mask[2] = { l_mask, r_mask };
    9367         3570 :       location_t *locs[2] = { i ? rl_loc : ll_loc, i ? rr_loc : lr_loc };
    9368              : 
    9369              :       /* Figure out the masks, and unshare the original operands.  */
    9370        10710 :       for (int j = 0; j < 2; j++)
    9371              :         {
    9372         7140 :           unsigned prec = TYPE_PRECISION (TREE_TYPE (op[j]));
    9373         7140 :           op[j] = unshare_expr (op[j]);
    9374              : 
    9375              :           /* Mask out the bits belonging to the other part.  */
    9376         7140 :           if (xmask[j][i].get_precision ())
    9377          102 :             mask[j] &= xmask[j][i];
    9378              : 
    9379         7140 :           if (shifted[j][i])
    9380              :             {
    9381          307 :               wide_int shift = wide_int::from (shifted[j][i], prec, UNSIGNED);
    9382          307 :               mask[j] = wi::lrshift (mask[j], shift);
    9383          307 :             }
    9384         7140 :           mask[j] = wide_int::from (mask[j], prec, UNSIGNED);
    9385              :         }
    9386              : 
    9387              :       /* Line up the operands for a compare.  */
    9388         3570 :       HOST_WIDE_INT shift = (toshift[0][i] - toshift[1][i]);
    9389              : 
    9390         3570 :       if (shift)
    9391              :         {
    9392           54 :           int j;
    9393           54 :           if (shift > 0)
    9394              :             j = 0;
    9395              :           else
    9396              :             {
    9397           52 :               j = 1;
    9398           52 :               shift = -shift;
    9399              :             }
    9400              : 
    9401           54 :           tree shiftsz = bitsize_int (shift);
    9402           54 :           op[j] = fold_build2_loc (locs[j][1], RSHIFT_EXPR, TREE_TYPE (op[j]),
    9403              :                                    op[j], shiftsz);
    9404           54 :           mask[j] = wi::lrshift (mask[j], shift);
    9405              :         }
    9406              : 
    9407              :       /* Convert to the smaller type before masking out unwanted
    9408              :          bits.  */
    9409         3570 :       tree type = TREE_TYPE (op[0]);
    9410         3570 :       if (type != TREE_TYPE (op[1]))
    9411              :         {
    9412          190 :           int j = (TYPE_PRECISION (type)
    9413          190 :                    < TYPE_PRECISION (TREE_TYPE (op[1])));
    9414          190 :           if (!j)
    9415           91 :             type = TREE_TYPE (op[1]);
    9416          190 :           op[j] = fold_convert_loc (locs[j][0], type, op[j]);
    9417          190 :           mask[j] = wide_int::from (mask[j], TYPE_PRECISION (type), UNSIGNED);
    9418              :         }
    9419              : 
    9420              :       /* Apply masks.  */
    9421        10710 :       for (int j = 0; j < 2; j++)
    9422         7140 :         if (mask[j] != wi::mask (0, true, mask[j].get_precision ()))
    9423         2766 :           op[j] = fold_build2_loc (locs[j][2], BIT_AND_EXPR, type,
    9424         5532 :                                    op[j], wide_int_to_tree (type, mask[j]));
    9425              : 
    9426         6961 :       cmp[i] = fold_build2_loc (i ? rloc : lloc, wanted_code, truth_type,
    9427              :                                 op[0], op[1]);
    9428        10710 :     }
    9429              : 
    9430              :   /* Reorder the compares if needed.  */
    9431         3391 :   if (first1)
    9432           45 :     std::swap (cmp[0], cmp[1]);
    9433              : 
    9434              :   /* Prepare to return the resulting compares.  Combine two parts if
    9435              :      needed.  */
    9436         3391 :   if (parts == 1)
    9437         3212 :     result = cmp[0];
    9438          179 :   else if (!separatep || !maybe_separate)
    9439              :     {
    9440              :       /* Only fold if any of the cmp is known, otherwise we may lose the
    9441              :          sequence point, and that may prevent further optimizations.  */
    9442          173 :       if (TREE_CODE (cmp[0]) == INTEGER_CST
    9443          137 :           || TREE_CODE (cmp[1]) == INTEGER_CST)
    9444           37 :         result = fold_build2_loc (rloc, orig_code, truth_type, cmp[0], cmp[1]);
    9445              :       else
    9446          136 :         result = build2_loc (rloc, orig_code, truth_type, cmp[0], cmp[1]);
    9447              :     }
    9448              :   else
    9449              :     {
    9450            6 :       result = cmp[0];
    9451            6 :       *separatep = cmp[1];
    9452              :     }
    9453              : 
    9454         3391 :   return result;
    9455       442844 : }
    9456              : 
    9457              : /* Try to simplify the AND of two comparisons, specified by
    9458              :    (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
    9459              :    If this can be simplified to a single expression (without requiring
    9460              :    introducing more SSA variables to hold intermediate values),
    9461              :    return the resulting tree.  Otherwise return NULL_TREE.
    9462              :    If the result expression is non-null, it has boolean type.
    9463              :    CODE is TRUTH_AND_EXPR if both comparisons are evaluated and
    9464              :    TRUTH_ANDIF_EXPR if the second one is only evaluated when the first
    9465              :    one is true; with -ftrapping-math the result raises an exception
    9466              :    for a NaN exactly when the evaluated comparisons do.  */
    9467              : 
    9468              : tree
    9469       613424 : maybe_fold_and_comparisons (tree type, enum tree_code code,
    9470              :                             enum tree_code code1, tree op1a, tree op1b,
    9471              :                             enum tree_code code2, tree op2a, tree op2b,
    9472              :                             basic_block outer_cond_bb)
    9473              : {
    9474       613424 :   if (tree t = and_comparisons_1 (type, code, code1, op1a, op1b,
    9475              :                                   code2, op2a, op2b, outer_cond_bb))
    9476              :     return t;
    9477              : 
    9478       611810 :   if (tree t = maybe_fold_comparisons_from_match_pd (type, BIT_AND_EXPR, code1,
    9479              :                                                      op1a, op1b, code2, op2a,
    9480              :                                                      op2b, outer_cond_bb))
    9481              :     return t;
    9482              : 
    9483              :   return NULL_TREE;
    9484              : }
    9485              : 
    9486              : /* Helper function for or_comparisons_1:  try to simplify the OR of the
    9487              :    ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
    9488              :    If INVERT is true, invert the value of VAR before doing the OR.
    9489              :    Return NULL_EXPR if we can't simplify this to a single expression.  */
    9490              : 
    9491              : static tree
    9492        31139 : or_var_with_comparison (tree type, enum tree_code code, tree var,
    9493              :                         bool invert, enum tree_code code2, tree op2a,
    9494              :                         tree op2b, basic_block outer_cond_bb)
    9495              : {
    9496        31139 :   tree t;
    9497        31139 :   gassign *stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (var));
    9498              : 
    9499              :   /* We can only deal with variables whose definitions are assignments.  */
    9500        31078 :   if (!stmt)
    9501              :     return NULL_TREE;
    9502              : 
    9503              :   /* If we have an inverted comparison, apply DeMorgan's law and rewrite
    9504              :      !var OR (op2a code2 op2b) => !(var AND !(op2a code2 op2b))
    9505              :      Then we only have to consider the simpler non-inverted cases.  */
    9506        31078 :   if (invert)
    9507              :     {
    9508        21944 :       code2 = invert_tree_comparison (code2, HONOR_NANS (op2a));
    9509        21944 :       if (code2 == ERROR_MARK)
    9510              :         return NULL_TREE;
    9511        21944 :       t = and_var_with_comparison_1 (type, truth_andor_compl (code), stmt,
    9512              :                                      code2, op2a, op2b, outer_cond_bb);
    9513              :     }
    9514              :   else
    9515         9134 :     t = or_var_with_comparison_1 (type, code, stmt, code2, op2a, op2b,
    9516              :                                   outer_cond_bb);
    9517        31078 :   return canonicalize_bool (t, invert);
    9518              : }
    9519              : 
    9520              : /* Try to simplify the OR of the ssa variable defined by the assignment
    9521              :    STMT with the comparison specified by (OP2A CODE2 OP2B).
    9522              :    Return NULL_EXPR if we can't simplify this to a single expression.  */
    9523              : 
    9524              : static tree
    9525        74179 : or_var_with_comparison_1 (tree type, enum tree_code code, gassign *stmt,
    9526              :                           enum tree_code code2, tree op2a, tree op2b,
    9527              :                           basic_block outer_cond_bb)
    9528              : {
    9529        74179 :   tree var = gimple_assign_lhs (stmt);
    9530        74179 :   tree true_test_var = NULL_TREE;
    9531        74179 :   tree false_test_var = NULL_TREE;
    9532        74179 :   enum tree_code innercode = gimple_assign_rhs_code (stmt);
    9533              : 
    9534              :   /* Check for identities like (var OR (var != 0)) => true .  */
    9535        74179 :   if (TREE_CODE (op2a) == SSA_NAME
    9536        74179 :       && TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE)
    9537              :     {
    9538        14775 :       if ((code2 == NE_EXPR && integer_zerop (op2b))
    9539        31763 :           || (code2 == EQ_EXPR && integer_nonzerop (op2b)))
    9540              :         {
    9541        12610 :           true_test_var = op2a;
    9542        12610 :           if (var == true_test_var)
    9543              :             return var;
    9544              :         }
    9545         2476 :       else if ((code2 == EQ_EXPR && integer_zerop (op2b))
    9546        15487 :                || (code2 == NE_EXPR && integer_nonzerop (op2b)))
    9547              :         {
    9548         4739 :           false_test_var = op2a;
    9549         4739 :           if (var == false_test_var)
    9550            0 :             return boolean_true_node;
    9551              :         }
    9552              :     }
    9553              : 
    9554              :   /* If the definition is a comparison, recurse on it.  */
    9555        74179 :   if (TREE_CODE_CLASS (innercode) == tcc_comparison)
    9556              :     {
    9557          652 :       tree t = or_comparisons_1 (type, code, innercode,
    9558              :                                  gimple_assign_rhs1 (stmt),
    9559              :                                  gimple_assign_rhs2 (stmt),
    9560              :                                  code2, op2a, op2b, outer_cond_bb);
    9561          326 :       if (t)
    9562              :         return t;
    9563              :     }
    9564              : 
    9565              :   /* If the definition is an AND or OR expression, we may be able to
    9566              :      simplify by reassociating.  */
    9567        74177 :   if (TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE
    9568        74177 :       && (innercode == BIT_AND_EXPR || innercode == BIT_IOR_EXPR))
    9569              :     {
    9570        23865 :       tree inner1 = gimple_assign_rhs1 (stmt);
    9571        23865 :       tree inner2 = gimple_assign_rhs2 (stmt);
    9572        23865 :       tree t;
    9573        23865 :       tree partial = NULL_TREE;
    9574        23865 :       bool is_or = (innercode == BIT_IOR_EXPR);
    9575              : 
    9576              :       /* Check for boolean identities that don't require recursive examination
    9577              :          of inner1/inner2:
    9578              :          inner1 OR (inner1 OR inner2) => inner1 OR inner2 => var
    9579              :          inner1 OR (inner1 AND inner2) => inner1
    9580              :          !inner1 OR (inner1 OR inner2) => true
    9581              :          !inner1 OR (inner1 AND inner2) => !inner1 OR inner2
    9582              :       */
    9583        23865 :       if (inner1 == true_test_var)
    9584            0 :         return (is_or ? var : inner1);
    9585        23865 :       else if (inner2 == true_test_var)
    9586            0 :         return (is_or ? var : inner2);
    9587        23865 :       else if (inner1 == false_test_var)
    9588            0 :         return (is_or
    9589            0 :                 ? boolean_true_node
    9590            0 :                 : or_var_with_comparison (type, code, inner2, false, code2,
    9591            0 :                                           op2a, op2b, outer_cond_bb));
    9592        23865 :       else if (inner2 == false_test_var)
    9593            0 :         return (is_or
    9594            0 :                 ? boolean_true_node
    9595            0 :                 : or_var_with_comparison (type, code, inner1, false, code2,
    9596            0 :                                           op2a, op2b, outer_cond_bb));
    9597              : 
    9598              :       /* Next, redistribute/reassociate the OR across the inner tests.
    9599              :          Compute the first partial result, (inner1 OR (op2a code op2b))  */
    9600        23865 :       gassign *s;
    9601        23865 :       if (TREE_CODE (inner1) == SSA_NAME
    9602        23865 :           && (s = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (inner1)))
    9603        23295 :           && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
    9604        39706 :           && (t = maybe_fold_or_comparisons (type, code,
    9605              :                                              gimple_assign_rhs_code (s),
    9606              :                                              gimple_assign_rhs1 (s),
    9607              :                                              gimple_assign_rhs2 (s),
    9608              :                                              code2, op2a, op2b,
    9609              :                                              outer_cond_bb)))
    9610              :         {
    9611              :           /* Handle the OR case, where we are reassociating:
    9612              :              (inner1 OR inner2) OR (op2a code2 op2b)
    9613              :              => (t OR inner2)
    9614              :              If the partial result t is a constant, we win.  Otherwise
    9615              :              continue on to try reassociating with the other inner test.  */
    9616           45 :           if (is_or)
    9617              :             {
    9618           17 :               if (integer_onep (t))
    9619            0 :                 return boolean_true_node;
    9620           17 :               else if (integer_zerop (t))
    9621              :                 return inner2;
    9622              :             }
    9623              : 
    9624              :           /* Handle the AND case, where we are redistributing:
    9625              :              (inner1 AND inner2) OR (op2a code2 op2b)
    9626              :              => (t AND (inner2 OR (op2a code op2b)))  */
    9627           28 :           else if (integer_zerop (t))
    9628            0 :             return boolean_false_node;
    9629              : 
    9630              :           /* Save partial result for later.  */
    9631              :           partial = t;
    9632              :         }
    9633              : 
    9634              :       /* Compute the second partial result, (inner2 OR (op2a code op2b)) */
    9635        23865 :       if (TREE_CODE (inner2) == SSA_NAME
    9636        23865 :           && (s = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (inner2)))
    9637        23459 :           && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
    9638        45538 :           && (t = maybe_fold_or_comparisons (type, code,
    9639              :                                              gimple_assign_rhs_code (s),
    9640              :                                              gimple_assign_rhs1 (s),
    9641              :                                              gimple_assign_rhs2 (s),
    9642              :                                              code2, op2a, op2b,
    9643              :                                              outer_cond_bb)))
    9644              :         {
    9645              :           /* Handle the OR case, where we are reassociating:
    9646              :              (inner1 OR inner2) OR (op2a code2 op2b)
    9647              :              => (inner1 OR t)
    9648              :              => (t OR partial)  */
    9649           60 :           if (is_or)
    9650              :             {
    9651           23 :               if (integer_zerop (t))
    9652              :                 return inner1;
    9653           23 :               else if (integer_onep (t))
    9654            0 :                 return boolean_true_node;
    9655              :               /* If both are the same, we can apply the identity
    9656              :                  (x OR x) == x.  */
    9657           23 :               else if (partial && same_bool_result_p (t, partial))
    9658            0 :                 return t;
    9659              :             }
    9660              : 
    9661              :           /* Handle the AND case, where we are redistributing:
    9662              :              (inner1 AND inner2) OR (op2a code2 op2b)
    9663              :              => (t AND (inner1 OR (op2a code2 op2b)))
    9664              :              => (t AND partial)  */
    9665              :           else
    9666              :             {
    9667           37 :               if (integer_zerop (t))
    9668            0 :                 return boolean_false_node;
    9669           37 :               else if (partial)
    9670              :                 {
    9671              :                   /* We already got a simplification for the other
    9672              :                      operand to the redistributed AND expression.  The
    9673              :                      interesting case is when at least one is true.
    9674              :                      Or, if both are the same, we can apply the identity
    9675              :                      (x AND x) == x.  */
    9676           14 :                   if (integer_onep (partial))
    9677              :                     return t;
    9678           13 :                   else if (integer_onep (t))
    9679              :                     return partial;
    9680            1 :                   else if (same_bool_result_p (t, partial))
    9681            0 :                     return t;
    9682              :                 }
    9683              :             }
    9684              :         }
    9685              :     }
    9686              :   return NULL_TREE;
    9687              : }
    9688              : 
    9689              : /* Try to simplify the OR of two comparisons defined by
    9690              :    (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
    9691              :    If this can be done without constructing an intermediate value,
    9692              :    return the resulting tree; otherwise NULL_TREE is returned.
    9693              :    This function is deliberately asymmetric as it recurses on SSA_DEFs
    9694              :    in the first comparison but not the second.  CODE is as for
    9695              :    maybe_fold_or_comparisons.  */
    9696              : 
    9697              : static tree
    9698       437981 : or_comparisons_1 (tree type, enum tree_code code,
    9699              :                   enum tree_code code1, tree op1a, tree op1b,
    9700              :                   enum tree_code code2, tree op2a, tree op2b,
    9701              :                   basic_block outer_cond_bb)
    9702              : {
    9703       437981 :   tree truth_type = truth_type_for (TREE_TYPE (op1a));
    9704              : 
    9705              :   /* First check for ((x CODE1 y) OR (x CODE2 y)).  */
    9706       437981 :   if (operand_equal_p (op1a, op2a, 0)
    9707       437981 :       && operand_equal_p (op1b, op2b, 0))
    9708              :     {
    9709              :       /* Result will be either NULL_TREE, or a combined comparison.  */
    9710         2462 :       tree t = combine_comparisons (UNKNOWN_LOCATION,
    9711              :                                     code, code1, code2,
    9712              :                                     truth_type, truth_type, op1a, op1b);
    9713         2462 :       if (t)
    9714              :         return t;
    9715              :     }
    9716              : 
    9717              :   /* Likewise the swapped case of the above.  */
    9718       435633 :   if (operand_equal_p (op1a, op2b, 0)
    9719       435633 :       && operand_equal_p (op1b, op2a, 0))
    9720              :     {
    9721              :       /* Result will be either NULL_TREE, or a combined comparison.  */
    9722            0 :       tree t = combine_comparisons (UNKNOWN_LOCATION,
    9723              :                                     code, code1,
    9724              :                                     swap_tree_comparison (code2),
    9725              :                                     truth_type, truth_type, op1a, op1b);
    9726            0 :       if (t)
    9727              :         return t;
    9728              :     }
    9729              : 
    9730              :   /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
    9731              :      NAME's definition is a truth value.  See if there are any simplifications
    9732              :      that can be done against the NAME's definition.  */
    9733       435633 :   if (TREE_CODE (op1a) == SSA_NAME
    9734       435558 :       && (code1 == NE_EXPR || code1 == EQ_EXPR)
    9735       739127 :       && (integer_zerop (op1b) || integer_onep (op1b)))
    9736              :     {
    9737        36699 :       bool invert = ((code1 == EQ_EXPR && integer_zerop (op1b))
    9738        60431 :                      || (code1 == NE_EXPR && integer_onep (op1b)));
    9739        56455 :       gimple *stmt = SSA_NAME_DEF_STMT (op1a);
    9740        56455 :       switch (gimple_code (stmt))
    9741              :         {
    9742        31053 :         case GIMPLE_ASSIGN:
    9743              :           /* Try to simplify by copy-propagating the definition.  */
    9744        31053 :           return or_var_with_comparison (type, code, op1a, invert, code2,
    9745        31053 :                                          op2a, op2b, outer_cond_bb);
    9746              : 
    9747        12967 :         case GIMPLE_PHI:
    9748              :           /* If every argument to the PHI produces the same result when
    9749              :              ORed with the second comparison, we win.
    9750              :              Do not do this unless the type is bool since we need a bool
    9751              :              result here anyway.  */
    9752        12967 :           if (TREE_CODE (TREE_TYPE (op1a)) == BOOLEAN_TYPE)
    9753              :             {
    9754              :               tree result = NULL_TREE;
    9755              :               unsigned i;
    9756          413 :               for (i = 0; i < gimple_phi_num_args (stmt); i++)
    9757              :                 {
    9758          413 :                   tree arg = gimple_phi_arg_def (stmt, i);
    9759              : 
    9760              :                   /* If this PHI has itself as an argument, ignore it.
    9761              :                      If all the other args produce the same result,
    9762              :                      we're still OK.  */
    9763          413 :                   if (arg == gimple_phi_result (stmt))
    9764            0 :                     continue;
    9765          413 :                   else if (TREE_CODE (arg) == INTEGER_CST)
    9766              :                     {
    9767          322 :                       if (invert ? integer_zerop (arg) : integer_nonzerop (arg))
    9768              :                         {
    9769          163 :                           if (!result)
    9770           45 :                             result = boolean_true_node;
    9771          118 :                           else if (!integer_onep (result))
    9772              :                             return NULL_TREE;
    9773              :                         }
    9774          159 :                       else if (!result)
    9775          119 :                         result = fold_build2 (code2, boolean_type_node,
    9776              :                                               op2a, op2b);
    9777           40 :                       else if (!same_bool_comparison_p (result,
    9778              :                                                         code2, op2a, op2b))
    9779              :                         return NULL_TREE;
    9780              :                     }
    9781           91 :                   else if (TREE_CODE (arg) == SSA_NAME
    9782           91 :                            && !SSA_NAME_IS_DEFAULT_DEF (arg))
    9783              :                     {
    9784           91 :                       tree temp;
    9785           91 :                       gimple *def_stmt = SSA_NAME_DEF_STMT (arg);
    9786              :                       /* In simple cases we can look through PHI nodes,
    9787              :                          but we have to be careful with loops.
    9788              :                          See PR49073.  */
    9789           91 :                       if (! dom_info_available_p (CDI_DOMINATORS)
    9790           91 :                           || gimple_bb (def_stmt) == gimple_bb (stmt)
    9791          182 :                           || dominated_by_p (CDI_DOMINATORS,
    9792           91 :                                              gimple_bb (def_stmt),
    9793           91 :                                              gimple_bb (stmt)))
    9794              :                         return NULL_TREE;
    9795           86 :                       temp = or_var_with_comparison (type, code, arg, invert,
    9796              :                                                      code2, op2a, op2b,
    9797              :                                                      outer_cond_bb);
    9798           86 :                       if (!temp)
    9799              :                         return NULL_TREE;
    9800            0 :                       else if (!result)
    9801              :                         result = temp;
    9802            0 :                       else if (!same_bool_result_p (result, temp))
    9803              :                         return NULL_TREE;
    9804              :                     }
    9805              :                   else
    9806              :                     return NULL_TREE;
    9807              :                 }
    9808              :               return result;
    9809              :             }
    9810              : 
    9811              :         default:
    9812              :           break;
    9813              :         }
    9814              :     }
    9815              :   return NULL_TREE;
    9816              : }
    9817              : 
    9818              : /* Try to simplify the OR of two comparisons, specified by
    9819              :    (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
    9820              :    If this can be simplified to a single expression (without requiring
    9821              :    introducing more SSA variables to hold intermediate values),
    9822              :    return the resulting tree.  Otherwise return NULL_TREE.
    9823              :    If the result expression is non-null, it has boolean type.
    9824              :    CODE is TRUTH_OR_EXPR if both comparisons are evaluated and
    9825              :    TRUTH_ORIF_EXPR if the second one is only evaluated when the first
    9826              :    one is false, as for maybe_fold_and_comparisons.  */
    9827              : 
    9828              : tree
    9829       238726 : maybe_fold_or_comparisons (tree type, enum tree_code code,
    9830              :                            enum tree_code code1, tree op1a, tree op1b,
    9831              :                            enum tree_code code2, tree op2a, tree op2b,
    9832              :                            basic_block outer_cond_bb)
    9833              : {
    9834       238726 :   if (tree t = or_comparisons_1 (type, code, code1, op1a, op1b,
    9835              :                                  code2, op2a, op2b, outer_cond_bb))
    9836              :     return t;
    9837              : 
    9838              :   /* Also try the other order, unless the second comparison is only
    9839              :      evaluated when the first one is false.  */
    9840       236379 :   if (code == TRUTH_OR_EXPR)
    9841       198929 :     if (tree t = or_comparisons_1 (type, code, code2, op2a, op2b,
    9842              :                                    code1, op1a, op1b, outer_cond_bb))
    9843              :       return t;
    9844              : 
    9845       236378 :   if (tree t = maybe_fold_comparisons_from_match_pd (type, BIT_IOR_EXPR, code1,
    9846              :                                                      op1a, op1b, code2, op2a,
    9847              :                                                      op2b, outer_cond_bb))
    9848              :     return t;
    9849              : 
    9850              :   return NULL_TREE;
    9851              : }
    9852              : 
    9853              : /* Fold STMT to a constant using VALUEIZE to valueize SSA names.
    9854              : 
    9855              :    Either NULL_TREE, a simplified but non-constant or a constant
    9856              :    is returned.
    9857              : 
    9858              :    ???  This should go into a gimple-fold-inline.h file to be eventually
    9859              :    privatized with the single valueize function used in the various TUs
    9860              :    to avoid the indirect function call overhead.  */
    9861              : 
    9862              : tree
    9863    438458369 : gimple_fold_stmt_to_constant_1 (gimple *stmt, tree (*valueize) (tree),
    9864              :                                 tree (*gvalueize) (tree))
    9865              : {
    9866    438458369 :   gimple_match_op res_op;
    9867              :   /* ???  The SSA propagators do not correctly deal with following SSA use-def
    9868              :      edges if there are intermediate VARYING defs.  For this reason
    9869              :      do not follow SSA edges here even though SCCVN can technically
    9870              :      just deal fine with that.  */
    9871    438458369 :   if (gimple_simplify (stmt, &res_op, NULL, gvalueize, valueize))
    9872              :     {
    9873     57908856 :       tree res = NULL_TREE;
    9874     57908856 :       if (gimple_simplified_result_is_gimple_val (&res_op))
    9875     35411299 :         res = res_op.ops[0];
    9876     22497557 :       else if (mprts_hook)
    9877      7886058 :         res = mprts_hook (&res_op);
    9878     43297357 :       if (res)
    9879              :         {
    9880     37336452 :           if (dump_file && dump_flags & TDF_DETAILS)
    9881              :             {
    9882         9026 :               fprintf (dump_file, "Match-and-simplified ");
    9883         9026 :               print_gimple_expr (dump_file, stmt, 0, TDF_SLIM);
    9884         9026 :               fprintf (dump_file, " to ");
    9885         9026 :               print_generic_expr (dump_file, res);
    9886         9026 :               fprintf (dump_file, "\n");
    9887              :             }
    9888              :           return res;
    9889              :         }
    9890              :     }
    9891              : 
    9892    401121917 :   location_t loc = gimple_location (stmt);
    9893    401121917 :   switch (gimple_code (stmt))
    9894              :     {
    9895    348188381 :     case GIMPLE_ASSIGN:
    9896    348188381 :       {
    9897    348188381 :         enum tree_code subcode = gimple_assign_rhs_code (stmt);
    9898              : 
    9899    348188381 :         switch (get_gimple_rhs_class (subcode))
    9900              :           {
    9901    118812626 :           case GIMPLE_SINGLE_RHS:
    9902    118812626 :             {
    9903    118812626 :               tree rhs = gimple_assign_rhs1 (stmt);
    9904    118812626 :               enum tree_code_class kind = TREE_CODE_CLASS (subcode);
    9905              : 
    9906    118812626 :               if (TREE_CODE (rhs) == SSA_NAME)
    9907              :                 {
    9908              :                   /* If the RHS is an SSA_NAME, return its known constant value,
    9909              :                      if any.  */
    9910     11810849 :                   return (*valueize) (rhs);
    9911              :                 }
    9912              :               /* Handle propagating invariant addresses into address
    9913              :                  operations.  */
    9914    107001777 :               else if (TREE_CODE (rhs) == ADDR_EXPR
    9915    107001777 :                        && !is_gimple_min_invariant (rhs))
    9916              :                 {
    9917      6327462 :                   poly_int64 offset = 0;
    9918      6327462 :                   tree base;
    9919      6327462 :                   base = get_addr_base_and_unit_offset_1 (TREE_OPERAND (rhs, 0),
    9920              :                                                           &offset,
    9921              :                                                           valueize);
    9922      6327462 :                   if (base
    9923      6327462 :                       && (CONSTANT_CLASS_P (base)
    9924      5650820 :                           || decl_address_invariant_p (base)))
    9925       202751 :                     return build_invariant_address (TREE_TYPE (rhs),
    9926       202751 :                                                     base, offset);
    9927              :                 }
    9928    100674315 :               else if (TREE_CODE (rhs) == CONSTRUCTOR
    9929      1156222 :                        && TREE_CODE (TREE_TYPE (rhs)) == VECTOR_TYPE
    9930    102299942 :                        && known_eq (CONSTRUCTOR_NELTS (rhs),
    9931              :                                     TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs))))
    9932              :                 {
    9933       446549 :                   unsigned i, nelts;
    9934       446549 :                   tree val;
    9935              : 
    9936       446549 :                   nelts = CONSTRUCTOR_NELTS (rhs);
    9937       446549 :                   tree_vector_builder vec (TREE_TYPE (rhs), nelts, 1);
    9938      1000229 :                   FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), i, val)
    9939              :                     {
    9940       541159 :                       val = (*valueize) (val);
    9941       541159 :                       if (TREE_CODE (val) == INTEGER_CST
    9942       456030 :                           || TREE_CODE (val) == REAL_CST
    9943       434028 :                           || TREE_CODE (val) == FIXED_CST)
    9944       107131 :                         vec.quick_push (val);
    9945              :                       else
    9946              :                         return NULL_TREE;
    9947              :                     }
    9948              : 
    9949        12521 :                   return vec.build ();
    9950       446549 :                 }
    9951    106352477 :               if (subcode == OBJ_TYPE_REF)
    9952              :                 {
    9953       389904 :                   tree val = (*valueize) (OBJ_TYPE_REF_EXPR (rhs));
    9954              :                   /* If callee is constant, we can fold away the wrapper.  */
    9955       389904 :                   if (is_gimple_min_invariant (val))
    9956              :                     return val;
    9957              :                 }
    9958              : 
    9959    106352185 :               if (kind == tcc_reference)
    9960              :                 {
    9961     70529835 :                   if ((TREE_CODE (rhs) == VIEW_CONVERT_EXPR
    9962     68235076 :                        || TREE_CODE (rhs) == REALPART_EXPR
    9963     67179995 :                        || TREE_CODE (rhs) == IMAGPART_EXPR)
    9964     72792791 :                       && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
    9965              :                     {
    9966      3621466 :                       tree val = (*valueize) (TREE_OPERAND (rhs, 0));
    9967      3621466 :                       return fold_unary_loc (EXPR_LOCATION (rhs),
    9968      3621466 :                                              TREE_CODE (rhs),
    9969      7242932 :                                              TREE_TYPE (rhs), val);
    9970              :                     }
    9971     66908369 :                   else if (TREE_CODE (rhs) == BIT_FIELD_REF
    9972     66908369 :                            && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
    9973              :                     {
    9974       679758 :                       tree val = (*valueize) (TREE_OPERAND (rhs, 0));
    9975       679758 :                       return fold_ternary_loc (EXPR_LOCATION (rhs),
    9976       679758 :                                                TREE_CODE (rhs),
    9977       679758 :                                                TREE_TYPE (rhs), val,
    9978       679758 :                                                TREE_OPERAND (rhs, 1),
    9979      1359516 :                                                TREE_OPERAND (rhs, 2));
    9980              :                     }
    9981     66228611 :                   else if (TREE_CODE (rhs) == MEM_REF
    9982     66228611 :                            && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
    9983              :                     {
    9984     13620384 :                       tree val = (*valueize) (TREE_OPERAND (rhs, 0));
    9985     13620384 :                       if (TREE_CODE (val) == ADDR_EXPR
    9986     13620384 :                           && is_gimple_min_invariant (val))
    9987              :                         {
    9988       980955 :                           tree tem = fold_build2 (MEM_REF, TREE_TYPE (rhs),
    9989              :                                                   unshare_expr (val),
    9990              :                                                   TREE_OPERAND (rhs, 1));
    9991       980955 :                           if (tem)
    9992     66228611 :                             rhs = tem;
    9993              :                         }
    9994              :                     }
    9995     66228611 :                   return fold_const_aggregate_ref_1 (rhs, valueize);
    9996              :                 }
    9997     35822350 :               else if (kind == tcc_declaration)
    9998      7993934 :                 return get_symbol_constant_value (rhs);
    9999              :               return rhs;
   10000              :             }
   10001              : 
   10002              :           case GIMPLE_UNARY_RHS:
   10003              :             return NULL_TREE;
   10004              : 
   10005    172111362 :           case GIMPLE_BINARY_RHS:
   10006              :             /* Translate &x + CST into an invariant form suitable for
   10007              :                further propagation.  */
   10008    172111362 :             if (subcode == POINTER_PLUS_EXPR)
   10009              :               {
   10010     25551625 :                 tree op0 = (*valueize) (gimple_assign_rhs1 (stmt));
   10011     25551625 :                 tree op1 = (*valueize) (gimple_assign_rhs2 (stmt));
   10012     25551625 :                 if (TREE_CODE (op0) == ADDR_EXPR
   10013      5844610 :                     && TREE_CODE (op1) == INTEGER_CST)
   10014              :                   {
   10015       588126 :                     tree off = fold_convert (ptr_type_node, op1);
   10016       588126 :                     return build1_loc
   10017      1176252 :                         (loc, ADDR_EXPR, TREE_TYPE (op0),
   10018       588126 :                          fold_build2 (MEM_REF,
   10019              :                                       TREE_TYPE (TREE_TYPE (op0)),
   10020       588126 :                                       unshare_expr (op0), off));
   10021              :                   }
   10022              :               }
   10023              :             /* Canonicalize bool != 0 and bool == 0 appearing after
   10024              :                valueization.  While gimple_simplify handles this
   10025              :                it can get confused by the ~X == 1 -> X == 0 transform
   10026              :                which we cant reduce to a SSA name or a constant
   10027              :                (and we have no way to tell gimple_simplify to not
   10028              :                consider those transforms in the first place).  */
   10029    146559737 :             else if (subcode == EQ_EXPR
   10030    146559737 :                      || subcode == NE_EXPR)
   10031              :               {
   10032      3463821 :                 tree lhs = gimple_assign_lhs (stmt);
   10033      3463821 :                 tree op0 = gimple_assign_rhs1 (stmt);
   10034      3463821 :                 if (useless_type_conversion_p (TREE_TYPE (lhs),
   10035      3463821 :                                                TREE_TYPE (op0)))
   10036              :                   {
   10037        24529 :                     tree op1 = (*valueize) (gimple_assign_rhs2 (stmt));
   10038        24529 :                     op0 = (*valueize) (op0);
   10039        24529 :                     if (TREE_CODE (op0) == INTEGER_CST)
   10040          518 :                       std::swap (op0, op1);
   10041        24529 :                     if (TREE_CODE (op1) == INTEGER_CST
   10042        24529 :                         && ((subcode == NE_EXPR && integer_zerop (op1))
   10043         2078 :                             || (subcode == EQ_EXPR && integer_onep (op1))))
   10044          276 :                       return op0;
   10045              :                   }
   10046              :               }
   10047              :             return NULL_TREE;
   10048              : 
   10049       773443 :           case GIMPLE_TERNARY_RHS:
   10050       773443 :             {
   10051              :               /* Handle ternary operators that can appear in GIMPLE form.  */
   10052       773443 :               tree op0 = (*valueize) (gimple_assign_rhs1 (stmt));
   10053       773443 :               tree op1 = (*valueize) (gimple_assign_rhs2 (stmt));
   10054       773443 :               tree op2 = (*valueize) (gimple_assign_rhs3 (stmt));
   10055       773443 :               return fold_ternary_loc (loc, subcode,
   10056       773443 :                                        TREE_TYPE (gimple_assign_lhs (stmt)),
   10057       773443 :                                        op0, op1, op2);
   10058              :             }
   10059              : 
   10060            0 :           default:
   10061            0 :             gcc_unreachable ();
   10062              :           }
   10063              :       }
   10064              : 
   10065     14993972 :     case GIMPLE_CALL:
   10066     14993972 :       {
   10067     14993972 :         tree fn;
   10068     14993972 :         gcall *call_stmt = as_a <gcall *> (stmt);
   10069              : 
   10070     14993972 :         if (gimple_call_internal_p (stmt))
   10071              :           {
   10072      1385984 :             enum tree_code subcode = ERROR_MARK;
   10073      1385984 :             switch (gimple_call_internal_fn (stmt))
   10074              :               {
   10075              :               case IFN_UBSAN_CHECK_ADD:
   10076              :                 subcode = PLUS_EXPR;
   10077              :                 break;
   10078         8517 :               case IFN_UBSAN_CHECK_SUB:
   10079         8517 :                 subcode = MINUS_EXPR;
   10080         8517 :                 break;
   10081         6857 :               case IFN_UBSAN_CHECK_MUL:
   10082         6857 :                 subcode = MULT_EXPR;
   10083         6857 :                 break;
   10084       150565 :               case IFN_BUILTIN_EXPECT:
   10085       150565 :                   {
   10086       150565 :                     tree arg0 = gimple_call_arg (stmt, 0);
   10087       150565 :                     tree op0 = (*valueize) (arg0);
   10088       150565 :                     if (TREE_CODE (op0) == INTEGER_CST)
   10089              :                       return op0;
   10090       118132 :                     return NULL_TREE;
   10091              :                   }
   10092              :               default:
   10093              :                 return NULL_TREE;
   10094              :               }
   10095        23671 :             tree arg0 = gimple_call_arg (stmt, 0);
   10096        23671 :             tree arg1 = gimple_call_arg (stmt, 1);
   10097        23671 :             tree op0 = (*valueize) (arg0);
   10098        23671 :             tree op1 = (*valueize) (arg1);
   10099              : 
   10100        23671 :             if (TREE_CODE (op0) != INTEGER_CST
   10101         2621 :                 || TREE_CODE (op1) != INTEGER_CST)
   10102              :               {
   10103        23149 :                 switch (subcode)
   10104              :                   {
   10105         6757 :                   case MULT_EXPR:
   10106              :                     /* x * 0 = 0 * x = 0 without overflow.  */
   10107         6757 :                     if (integer_zerop (op0) || integer_zerop (op1))
   10108           20 :                       return build_zero_cst (TREE_TYPE (arg0));
   10109              :                     break;
   10110         8175 :                   case MINUS_EXPR:
   10111              :                     /* y - y = 0 without overflow.  */
   10112         8175 :                     if (operand_equal_p (op0, op1, 0))
   10113            0 :                       return build_zero_cst (TREE_TYPE (arg0));
   10114              :                     break;
   10115              :                   default:
   10116              :                     break;
   10117              :                   }
   10118              :               }
   10119        23651 :             tree res
   10120        23651 :               = fold_binary_loc (loc, subcode, TREE_TYPE (arg0), op0, op1);
   10121        23651 :             if (res
   10122         3144 :                 && TREE_CODE (res) == INTEGER_CST
   10123        24173 :                 && !TREE_OVERFLOW (res))
   10124            0 :               return res;
   10125              :             return NULL_TREE;
   10126              :           }
   10127              : 
   10128     13607988 :         fn = (*valueize) (gimple_call_fn (stmt));
   10129     13607988 :         if (TREE_CODE (fn) == ADDR_EXPR
   10130     12966527 :             && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
   10131     12966463 :             && fndecl_built_in_p (TREE_OPERAND (fn, 0))
   10132     19916325 :             && gimple_builtin_call_types_compatible_p (stmt,
   10133      6308337 :                                                        TREE_OPERAND (fn, 0)))
   10134              :           {
   10135      6203519 :             tree *args = XALLOCAVEC (tree, gimple_call_num_args (stmt));
   10136      6203519 :             tree retval;
   10137      6203519 :             unsigned i;
   10138     19136258 :             for (i = 0; i < gimple_call_num_args (stmt); ++i)
   10139     12932739 :               args[i] = (*valueize) (gimple_call_arg (stmt, i));
   10140      6203519 :             retval = fold_builtin_call_array (loc,
   10141              :                                          gimple_call_return_type (call_stmt),
   10142              :                                          fn, gimple_call_num_args (stmt), args);
   10143      6203519 :             if (retval)
   10144              :               {
   10145              :                 /* fold_call_expr wraps the result inside a NOP_EXPR.  */
   10146        61958 :                 STRIP_NOPS (retval);
   10147        61958 :                 retval = fold_convert (gimple_call_return_type (call_stmt),
   10148              :                                        retval);
   10149              :               }
   10150              :             return retval;
   10151              :           }
   10152              :         return NULL_TREE;
   10153              :       }
   10154              : 
   10155              :     default:
   10156              :       return NULL_TREE;
   10157              :     }
   10158              : }
   10159              : 
   10160              : /* Fold STMT to a constant using VALUEIZE to valueize SSA names.
   10161              :    Returns NULL_TREE if folding to a constant is not possible, otherwise
   10162              :    returns a constant according to is_gimple_min_invariant.  */
   10163              : 
   10164              : tree
   10165         4384 : gimple_fold_stmt_to_constant (gimple *stmt, tree (*valueize) (tree))
   10166              : {
   10167         4384 :   tree res = gimple_fold_stmt_to_constant_1 (stmt, valueize);
   10168         4384 :   if (res && is_gimple_min_invariant (res))
   10169         2369 :     return res;
   10170              :   return NULL_TREE;
   10171              : }
   10172              : 
   10173              : 
   10174              : /* The following set of functions are supposed to fold references using
   10175              :    their constant initializers.  */
   10176              : 
   10177              : /* See if we can find constructor defining value of BASE.
   10178              :    When we know the constructor with constant offset (such as
   10179              :    base is array[40] and we do know constructor of array), then
   10180              :    BIT_OFFSET is adjusted accordingly.
   10181              : 
   10182              :    As a special case, return error_mark_node when constructor
   10183              :    is not explicitly available, but it is known to be zero
   10184              :    such as 'static const int a;'.  */
   10185              : static tree
   10186    126043957 : get_base_constructor (tree base, poly_int64 *bit_offset,
   10187              :                       tree (*valueize)(tree))
   10188              : {
   10189    126152005 :   poly_int64 bit_offset2, size, max_size;
   10190    126152005 :   bool reverse;
   10191              : 
   10192    126152005 :   if (TREE_CODE (base) == MEM_REF)
   10193              :     {
   10194    134281102 :       poly_offset_int boff = *bit_offset + mem_ref_offset (base) * BITS_PER_UNIT;
   10195     67140551 :       if (!boff.to_shwi (bit_offset))
   10196     66769597 :         return NULL_TREE;
   10197              : 
   10198     67140199 :       if (valueize
   10199     67140199 :           && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
   10200     35220354 :         base = valueize (TREE_OPERAND (base, 0));
   10201     67140199 :       if (!base || TREE_CODE (base) != ADDR_EXPR)
   10202              :         return NULL_TREE;
   10203       370954 :       base = TREE_OPERAND (base, 0);
   10204              :     }
   10205     59011454 :   else if (valueize
   10206     30652287 :            && TREE_CODE (base) == SSA_NAME)
   10207            0 :     base = valueize (base);
   10208              : 
   10209              :   /* Get a CONSTRUCTOR.  If BASE is a VAR_DECL, get its
   10210              :      DECL_INITIAL.  If BASE is a nested reference into another
   10211              :      ARRAY_REF or COMPONENT_REF, make a recursive call to resolve
   10212              :      the inner reference.  */
   10213     59382408 :   switch (TREE_CODE (base))
   10214              :     {
   10215     51697452 :     case VAR_DECL:
   10216     51697452 :     case CONST_DECL:
   10217     51697452 :       {
   10218     51697452 :         tree init = ctor_for_folding (base);
   10219              : 
   10220              :         /* Our semantic is exact opposite of ctor_for_folding;
   10221              :            NULL means unknown, while error_mark_node is 0.  */
   10222     51697452 :         if (init == error_mark_node)
   10223              :           return NULL_TREE;
   10224      1310165 :         if (!init)
   10225         1314 :           return error_mark_node;
   10226              :         return init;
   10227              :       }
   10228              : 
   10229       108048 :     case VIEW_CONVERT_EXPR:
   10230       108048 :       return get_base_constructor (TREE_OPERAND (base, 0),
   10231       108048 :                                    bit_offset, valueize);
   10232              : 
   10233       360647 :     case ARRAY_REF:
   10234       360647 :     case COMPONENT_REF:
   10235       360647 :       base = get_ref_base_and_extent (base, &bit_offset2, &size, &max_size,
   10236              :                                       &reverse);
   10237       360647 :       if (!known_size_p (max_size) || maybe_ne (size, max_size))
   10238              :         return NULL_TREE;
   10239       299107 :       *bit_offset +=  bit_offset2;
   10240       299107 :       return get_base_constructor (base, bit_offset, valueize);
   10241              : 
   10242              :     case CONSTRUCTOR:
   10243              :       return base;
   10244              : 
   10245      7216261 :     default:
   10246      7216261 :       if (CONSTANT_CLASS_P (base))
   10247        95042 :         return base;
   10248              : 
   10249              :       return NULL_TREE;
   10250              :     }
   10251              : }
   10252              : 
   10253              : /* CTOR is a CONSTRUCTOR of an array or vector type.  Fold a reference of SIZE
   10254              :    bits to the memory at bit OFFSET.  If non-null, TYPE is the expected type of
   10255              :    the reference; otherwise the type of the referenced element is used instead.
   10256              :    When SIZE is zero, attempt to fold a reference to the entire element OFFSET
   10257              :    refers to.  Increment *SUBOFF by the bit offset of the accessed element.  */
   10258              : 
   10259              : static tree
   10260       723740 : fold_array_ctor_reference (tree type, tree ctor,
   10261              :                            unsigned HOST_WIDE_INT offset,
   10262              :                            unsigned HOST_WIDE_INT size,
   10263              :                            tree from_decl,
   10264              :                            unsigned HOST_WIDE_INT *suboff)
   10265              : {
   10266       723740 :   offset_int low_bound;
   10267       723740 :   offset_int elt_size;
   10268       723740 :   offset_int access_index;
   10269       723740 :   tree domain_type = NULL_TREE;
   10270       723740 :   HOST_WIDE_INT inner_offset;
   10271              : 
   10272              :   /* Compute low bound and elt size.  */
   10273       723740 :   if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
   10274       723740 :     domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
   10275       723740 :   if (domain_type && TYPE_MIN_VALUE (domain_type))
   10276              :     {
   10277              :       /* Static constructors for variably sized objects make no sense.  */
   10278       723740 :       if (TREE_CODE (TYPE_MIN_VALUE (domain_type)) != INTEGER_CST)
   10279              :         return NULL_TREE;
   10280       723740 :       low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
   10281              :     }
   10282              :   else
   10283            0 :     low_bound = 0;
   10284              :   /* Static constructors for variably sized objects make no sense.  */
   10285       723740 :   if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor)))) != INTEGER_CST)
   10286              :     return NULL_TREE;
   10287       723740 :   elt_size = wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor))));
   10288              : 
   10289              :   /* When TYPE is non-null, verify that it specifies a constant-sized
   10290              :      access of a multiple of the array element size.  Avoid division
   10291              :      by zero below when ELT_SIZE is zero, such as with the result of
   10292              :      an initializer for a zero-length array or an empty struct.  */
   10293       723776 :   if (elt_size == 0
   10294       723740 :       || (type
   10295       723704 :           && (!TYPE_SIZE_UNIT (type)
   10296       723704 :               || TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)))
   10297              :     return NULL_TREE;
   10298              : 
   10299              :   /* Compute the array index we look for.  */
   10300       723704 :   access_index = wi::udiv_trunc (offset_int (offset / BITS_PER_UNIT),
   10301              :                                  elt_size);
   10302       723704 :   access_index += low_bound;
   10303              : 
   10304              :   /* And offset within the access.  */
   10305       723704 :   inner_offset = offset % (elt_size.to_uhwi () * BITS_PER_UNIT);
   10306              : 
   10307       723704 :   unsigned HOST_WIDE_INT elt_sz = elt_size.to_uhwi ();
   10308       723704 :   if (size > elt_sz * BITS_PER_UNIT)
   10309              :     {
   10310              :       /* native_encode_expr constraints.  */
   10311        22322 :       if (size > MAX_BITSIZE_MODE_ANY_MODE
   10312        18344 :           || size % BITS_PER_UNIT != 0
   10313        18344 :           || inner_offset % BITS_PER_UNIT != 0
   10314        18344 :           || elt_sz > MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT)
   10315              :         return NULL_TREE;
   10316              : 
   10317        18344 :       unsigned ctor_idx;
   10318        18344 :       tree val = get_array_ctor_element_at_index (ctor, access_index,
   10319              :                                                   &ctor_idx);
   10320        18368 :       if (!val && ctor_idx >= CONSTRUCTOR_NELTS (ctor))
   10321           23 :         return build_zero_cst (type);
   10322              : 
   10323              :       /* native-encode adjacent ctor elements.  */
   10324        18321 :       unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
   10325        18321 :       unsigned bufoff = 0;
   10326        18321 :       offset_int index = 0;
   10327        18321 :       offset_int max_index = access_index;
   10328        18321 :       constructor_elt *elt = CONSTRUCTOR_ELT (ctor, ctor_idx);
   10329        18321 :       if (!val)
   10330            1 :         val = build_zero_cst (TREE_TYPE (TREE_TYPE (ctor)));
   10331        18320 :       else if (!CONSTANT_CLASS_P (val))
   10332              :         return NULL_TREE;
   10333        18150 :       if (!elt->index)
   10334              :         ;
   10335        18014 :       else if (TREE_CODE (elt->index) == RANGE_EXPR)
   10336              :         {
   10337           92 :           index = wi::to_offset (TREE_OPERAND (elt->index, 0));
   10338           92 :           max_index = wi::to_offset (TREE_OPERAND (elt->index, 1));
   10339              :         }
   10340              :       else
   10341        17922 :         index = max_index = wi::to_offset (elt->index);
   10342        18150 :       index = wi::umax (index, access_index);
   10343       120071 :       do
   10344              :         {
   10345       120071 :           if (bufoff + elt_sz > sizeof (buf))
   10346            0 :             elt_sz = sizeof (buf) - bufoff;
   10347       120071 :           int len;
   10348       120071 :           if (TREE_CODE (val) == RAW_DATA_CST)
   10349              :             {
   10350           20 :               gcc_assert (inner_offset == 0);
   10351           20 :               if (!elt->index || TREE_CODE (elt->index) != INTEGER_CST)
   10352              :                 return NULL_TREE;
   10353           40 :               inner_offset = (access_index
   10354           20 :                               - wi::to_offset (elt->index)).to_uhwi ();
   10355           20 :               len = MIN (sizeof (buf) - bufoff,
   10356              :                          (unsigned) (RAW_DATA_LENGTH (val) - inner_offset));
   10357           20 :               memcpy (buf + bufoff, RAW_DATA_POINTER (val) + inner_offset,
   10358              :                       len);
   10359           20 :               access_index += len - 1;
   10360              :             }
   10361              :           else
   10362              :             {
   10363       240102 :               len = native_encode_expr (val, buf + bufoff, elt_sz,
   10364       120051 :                                         inner_offset / BITS_PER_UNIT);
   10365       120051 :               if (len != (int) elt_sz - inner_offset / BITS_PER_UNIT)
   10366              :                 return NULL_TREE;
   10367              :             }
   10368       120071 :           inner_offset = 0;
   10369       120071 :           bufoff += len;
   10370              : 
   10371       120071 :           access_index += 1;
   10372       120071 :           if (wi::cmpu (access_index, index) == 0)
   10373            2 :             val = elt->value;
   10374       120069 :           else if (wi::cmpu (access_index, max_index) > 0)
   10375              :             {
   10376       118829 :               ctor_idx++;
   10377       118829 :               if (ctor_idx >= CONSTRUCTOR_NELTS (ctor))
   10378              :                 {
   10379        16102 :                   val = build_zero_cst (TREE_TYPE (TREE_TYPE (ctor)));
   10380        16102 :                   ++max_index;
   10381              :                 }
   10382              :               else
   10383              :                 {
   10384       102727 :                   elt = CONSTRUCTOR_ELT (ctor, ctor_idx);
   10385       102727 :                   index = 0;
   10386       102727 :                   max_index = access_index;
   10387       102727 :                   if (!elt->index)
   10388              :                     ;
   10389       101911 :                   else if (TREE_CODE (elt->index) == RANGE_EXPR)
   10390              :                     {
   10391            0 :                       index = wi::to_offset (TREE_OPERAND (elt->index, 0));
   10392            0 :                       max_index = wi::to_offset (TREE_OPERAND (elt->index, 1));
   10393              :                     }
   10394              :                   else
   10395       101911 :                     index = max_index = wi::to_offset (elt->index);
   10396       102727 :                   index = wi::umax (index, access_index);
   10397       102727 :                   if (wi::cmpu (access_index, index) == 0)
   10398       102722 :                     val = elt->value;
   10399              :                   else
   10400            5 :                     val = build_zero_cst (TREE_TYPE (TREE_TYPE (ctor)));
   10401              :                 }
   10402              :             }
   10403              :         }
   10404       120071 :       while (bufoff < size / BITS_PER_UNIT);
   10405        18150 :       *suboff += size;
   10406        18150 :       return native_interpret_expr (type, buf, size / BITS_PER_UNIT);
   10407              :     }
   10408              : 
   10409       701382 :   unsigned ctor_idx;
   10410       701382 :   if (tree val = get_array_ctor_element_at_index (ctor, access_index,
   10411              :                                                   &ctor_idx))
   10412              :     {
   10413       700227 :       if (TREE_CODE (val) == RAW_DATA_CST)
   10414              :         {
   10415         2590 :           if (size != BITS_PER_UNIT || elt_sz != 1 || inner_offset != 0)
   10416              :             return NULL_TREE;
   10417         2582 :           constructor_elt *elt = CONSTRUCTOR_ELT (ctor, ctor_idx);
   10418         2582 :           if (elt->index == NULL_TREE || TREE_CODE (elt->index) != INTEGER_CST)
   10419              :             return NULL_TREE;
   10420         2582 :           unsigned o = (access_index - wi::to_offset (elt->index)).to_uhwi ();
   10421         2582 :           val = build_int_cst (TREE_TYPE (val), RAW_DATA_UCHAR_ELT (val, o));
   10422              :         }
   10423       700219 :       if (!size && TREE_CODE (val) != CONSTRUCTOR)
   10424              :         {
   10425              :           /* For the final reference to the entire accessed element
   10426              :              (SIZE is zero), reset INNER_OFFSET, disegard TYPE (which
   10427              :              may be null) in favor of the type of the element, and set
   10428              :              SIZE to the size of the accessed element.  */
   10429        24079 :           inner_offset = 0;
   10430        24079 :           type = TREE_TYPE (val);
   10431        24079 :           size = elt_sz * BITS_PER_UNIT;
   10432              :         }
   10433      1805528 :       else if (size && access_index < CONSTRUCTOR_NELTS (ctor) - 1
   10434       494123 :                && TREE_CODE (val) == CONSTRUCTOR
   10435        18215 :                && (elt_sz * BITS_PER_UNIT - inner_offset) < size)
   10436              :         /* If this isn't the last element in the CTOR and a CTOR itself
   10437              :            and it does not cover the whole object we are requesting give up
   10438              :            since we're not set up for combining from multiple CTORs.  */
   10439           26 :         return NULL_TREE;
   10440              : 
   10441       700193 :       *suboff += access_index.to_uhwi () * elt_sz * BITS_PER_UNIT;
   10442       700193 :       return fold_ctor_reference (type, val, inner_offset, size, from_decl,
   10443              :                                   suboff);
   10444              :     }
   10445              : 
   10446              :   /* Memory not explicitly mentioned in constructor is 0 (or
   10447              :      the reference is out of range).  */
   10448         1155 :   return type ? build_zero_cst (type) : NULL_TREE;
   10449              : }
   10450              : 
   10451              : /* CTOR is a CONSTRUCTOR of a record or union type.  Fold a reference of SIZE
   10452              :    bits to the memory at bit OFFSET.  If non-null, TYPE is the expected type of
   10453              :    the reference; otherwise the type of the referenced member is used instead.
   10454              :    When SIZE is zero, attempt to fold a reference to the entire member OFFSET
   10455              :    refers to.  Increment *SUBOFF by the bit offset of the accessed member.  */
   10456              : 
   10457              : static tree
   10458       114840 : fold_nonarray_ctor_reference (tree type, tree ctor,
   10459              :                               unsigned HOST_WIDE_INT offset,
   10460              :                               unsigned HOST_WIDE_INT size,
   10461              :                               tree from_decl,
   10462              :                               unsigned HOST_WIDE_INT *suboff)
   10463              : {
   10464       114840 :   unsigned HOST_WIDE_INT cnt;
   10465       114840 :   tree cfield, cval;
   10466              : 
   10467       163752 :   FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
   10468              :     {
   10469       154795 :       tree byte_offset = DECL_FIELD_OFFSET (cfield);
   10470       154795 :       tree field_offset = DECL_FIELD_BIT_OFFSET (cfield);
   10471       154795 :       tree field_size = DECL_SIZE (cfield);
   10472              : 
   10473       154795 :       if (!field_size)
   10474              :         {
   10475              :           /* Determine the size of the flexible array member from
   10476              :              the size of the initializer provided for it.  */
   10477          847 :           field_size = TYPE_SIZE (TREE_TYPE (cval));
   10478              :         }
   10479              : 
   10480              :       /* Variable sized objects in static constructors makes no sense,
   10481              :          but field_size can be NULL for flexible array members.  */
   10482       154795 :       gcc_assert (TREE_CODE (field_offset) == INTEGER_CST
   10483              :                   && TREE_CODE (byte_offset) == INTEGER_CST
   10484              :                   && (field_size != NULL_TREE
   10485              :                       ? TREE_CODE (field_size) == INTEGER_CST
   10486              :                       : TREE_CODE (TREE_TYPE (cfield)) == ARRAY_TYPE));
   10487              : 
   10488              :       /* Compute bit offset of the field.  */
   10489       154795 :       offset_int bitoffset
   10490       154795 :         = (wi::to_offset (field_offset)
   10491       154795 :            + (wi::to_offset (byte_offset) << LOG2_BITS_PER_UNIT));
   10492              :       /* Compute bit offset where the field ends.  */
   10493       154795 :       offset_int bitoffset_end;
   10494       154795 :       if (field_size != NULL_TREE)
   10495       154795 :         bitoffset_end = bitoffset + wi::to_offset (field_size);
   10496              :       else
   10497            0 :         bitoffset_end = 0;
   10498              : 
   10499              :       /* Compute the bit offset of the end of the desired access.
   10500              :          As a special case, if the size of the desired access is
   10501              :          zero, assume the access is to the entire field (and let
   10502              :          the caller make any necessary adjustments by storing
   10503              :          the actual bounds of the field in FIELDBOUNDS).  */
   10504       154795 :       offset_int access_end = offset_int (offset);
   10505       154795 :       if (size)
   10506       108170 :         access_end += size;
   10507              :       else
   10508        46625 :         access_end = bitoffset_end;
   10509              : 
   10510              :       /* Is there any overlap between the desired access at
   10511              :          [OFFSET, OFFSET+SIZE) and the offset of the field within
   10512              :          the object at [BITOFFSET, BITOFFSET_END)?  */
   10513       154795 :       if (wi::cmps (access_end, bitoffset) > 0
   10514       154795 :           && (field_size == NULL_TREE
   10515       152090 :               || wi::lts_p (offset, bitoffset_end)))
   10516              :         {
   10517       105883 :           *suboff += bitoffset.to_uhwi ();
   10518              : 
   10519       105883 :           if (!size && TREE_CODE (cval) != CONSTRUCTOR)
   10520              :             {
   10521              :               /* For the final reference to the entire accessed member
   10522              :                  (SIZE is zero), reset OFFSET, disegard TYPE (which may
   10523              :                  be null) in favor of the type of the member, and set
   10524              :                  SIZE to the size of the accessed member.  */
   10525        21470 :               offset = bitoffset.to_uhwi ();
   10526        21470 :               type = TREE_TYPE (cval);
   10527        21470 :               size = (bitoffset_end - bitoffset).to_uhwi ();
   10528              :             }
   10529              : 
   10530              :           /* We do have overlap.  Now see if the field is large enough
   10531              :              to cover the access.  Give up for accesses that extend
   10532              :              beyond the end of the object or that span multiple fields.  */
   10533       105883 :           if (wi::cmps (access_end, bitoffset_end) > 0)
   10534              :             return NULL_TREE;
   10535       105237 :           if (offset < bitoffset)
   10536              :             return NULL_TREE;
   10537              : 
   10538       105237 :           offset_int inner_offset = offset_int (offset) - bitoffset;
   10539              : 
   10540              :           /* Integral bit-fields are left-justified on big-endian targets, so
   10541              :              we must arrange for native_encode_int to start at their MSB.  */
   10542       105237 :           if (DECL_BIT_FIELD (cfield) && INTEGRAL_TYPE_P (TREE_TYPE (cfield)))
   10543              :             {
   10544       105237 :               if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
   10545              :                 return NULL_TREE;
   10546       105237 :               if (BYTES_BIG_ENDIAN)
   10547              :                 {
   10548              :                   tree ctype = TREE_TYPE (cfield);
   10549              :                   unsigned int encoding_size;
   10550              :                   if (TYPE_MODE (ctype) != BLKmode)
   10551              :                     encoding_size
   10552              :                       = GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (ctype));
   10553              :                   else
   10554              :                     encoding_size = TREE_INT_CST_LOW (TYPE_SIZE (ctype));
   10555              :                   inner_offset += encoding_size - wi::to_offset (field_size);
   10556              :                 }
   10557              :             }
   10558              : 
   10559       105237 :           return fold_ctor_reference (type, cval,
   10560       105237 :                                       inner_offset.to_uhwi (), size,
   10561              :                                       from_decl, suboff);
   10562              :         }
   10563              :     }
   10564              : 
   10565         8957 :   if (!type)
   10566              :     return NULL_TREE;
   10567              : 
   10568         8957 :   return build_zero_cst (type);
   10569              : }
   10570              : 
   10571              : /* CTOR is a value initializing memory.  Fold a reference of TYPE and
   10572              :    bit size POLY_SIZE to the memory at bit POLY_OFFSET.  When POLY_SIZE
   10573              :    is zero, attempt to fold a reference to the entire subobject
   10574              :    which OFFSET refers to.  This is used when folding accesses to
   10575              :    string members of aggregates.  When non-null, set *SUBOFF to
   10576              :    the bit offset of the accessed subobject.  */
   10577              : 
   10578              : tree
   10579      2169963 : fold_ctor_reference (tree type, tree ctor, const poly_uint64 &poly_offset,
   10580              :                      const poly_uint64 &poly_size, tree from_decl,
   10581              :                      unsigned HOST_WIDE_INT *suboff /* = NULL */)
   10582              : {
   10583      2169963 :   tree ret;
   10584              : 
   10585              :   /* We found the field with exact match.  */
   10586      2169963 :   if (type
   10587      2169963 :       && useless_type_conversion_p (type, TREE_TYPE (ctor))
   10588      3247410 :       && known_eq (poly_offset, 0U))
   10589      1076181 :     return canonicalize_constructor_val (unshare_expr (ctor), from_decl);
   10590              : 
   10591              :   /* The remaining optimizations need a constant size and offset.  */
   10592      1093782 :   unsigned HOST_WIDE_INT size, offset;
   10593      1093782 :   if (!poly_size.is_constant (&size) || !poly_offset.is_constant (&offset))
   10594              :     return NULL_TREE;
   10595              : 
   10596              :   /* We are at the end of walk, see if we can view convert the
   10597              :      result.  */
   10598      1093782 :   if (!AGGREGATE_TYPE_P (TREE_TYPE (ctor)) && !offset
   10599              :       /* VIEW_CONVERT_EXPR is defined only for matching sizes.  */
   10600        31305 :       && known_eq (wi::to_poly_widest (TYPE_SIZE (type)), size)
   10601        31198 :       && known_eq (wi::to_poly_widest (TYPE_SIZE (TREE_TYPE (ctor))), size))
   10602              :     {
   10603        20595 :       ret = canonicalize_constructor_val (unshare_expr (ctor), from_decl);
   10604        20595 :       if (ret)
   10605              :         {
   10606        20595 :           ret = fold_unary (VIEW_CONVERT_EXPR, type, ret);
   10607        20595 :           if (ret)
   10608        20575 :             STRIP_USELESS_TYPE_CONVERSION (ret);
   10609              :         }
   10610              :       return ret;
   10611              :     }
   10612              : 
   10613              :   /* For constants and byte-aligned/sized reads, try to go through
   10614              :      native_encode/interpret.  */
   10615      1073187 :   if (CONSTANT_CLASS_P (ctor)
   10616              :       && BITS_PER_UNIT == 8
   10617       226339 :       && offset % BITS_PER_UNIT == 0
   10618       226335 :       && offset / BITS_PER_UNIT <= INT_MAX
   10619       226303 :       && size % BITS_PER_UNIT == 0
   10620       226294 :       && size <= MAX_BITSIZE_MODE_ANY_MODE
   10621      1299183 :       && can_native_interpret_type_p (type))
   10622              :     {
   10623       210563 :       unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
   10624       421126 :       int len = native_encode_expr (ctor, buf, size / BITS_PER_UNIT,
   10625       210563 :                                     offset / BITS_PER_UNIT);
   10626       210563 :       if (len > 0)
   10627       209758 :         return native_interpret_expr (type, buf, len);
   10628              :     }
   10629              : 
   10630              :   /* For constructors, try first a recursive local processing, but in any case
   10631              :      this requires the native storage order.  */
   10632       863429 :   if (TREE_CODE (ctor) == CONSTRUCTOR
   10633       863429 :       && !(AGGREGATE_TYPE_P (TREE_TYPE (ctor))
   10634       838810 :            && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (ctor))))
   10635              :     {
   10636       838580 :       unsigned HOST_WIDE_INT dummy = 0;
   10637       838580 :       if (!suboff)
   10638       692412 :         suboff = &dummy;
   10639              : 
   10640       838580 :       tree ret;
   10641       838580 :       if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE
   10642       838580 :           || TREE_CODE (TREE_TYPE (ctor)) == VECTOR_TYPE)
   10643       723740 :         ret = fold_array_ctor_reference (type, ctor, offset, size,
   10644              :                                          from_decl, suboff);
   10645              :       else
   10646       114840 :         ret = fold_nonarray_ctor_reference (type, ctor, offset, size,
   10647              :                                             from_decl, suboff);
   10648              : 
   10649              :       /* Otherwise fall back to native_encode_initializer.  This may be done
   10650              :          only from the outermost fold_ctor_reference call (because it itself
   10651              :          recurses into CONSTRUCTORs and doesn't update suboff).  */
   10652       838580 :       if (ret == NULL_TREE
   10653       217315 :           && suboff == &dummy
   10654              :           && BITS_PER_UNIT == 8
   10655       208944 :           && offset % BITS_PER_UNIT == 0
   10656       208942 :           && offset / BITS_PER_UNIT <= INT_MAX
   10657       208942 :           && size % BITS_PER_UNIT == 0
   10658       208933 :           && size <= MAX_BITSIZE_MODE_ANY_MODE
   10659      1043535 :           && can_native_interpret_type_p (type))
   10660              :         {
   10661       190759 :           unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
   10662       381518 :           int len = native_encode_initializer (ctor, buf, size / BITS_PER_UNIT,
   10663       190759 :                                                offset / BITS_PER_UNIT);
   10664       190759 :           if (len > 0)
   10665         1333 :             return native_interpret_expr (type, buf, len);
   10666              :         }
   10667              : 
   10668              :       return ret;
   10669              :     }
   10670              : 
   10671              :   return NULL_TREE;
   10672              : }
   10673              : 
   10674              : /* Return the tree representing the element referenced by T if T is an
   10675              :    ARRAY_REF or COMPONENT_REF into constant aggregates valuezing SSA
   10676              :    names using VALUEIZE.  Return NULL_TREE otherwise.  */
   10677              : 
   10678              : tree
   10679    131399763 : fold_const_aggregate_ref_1 (tree t, tree (*valueize) (tree))
   10680              : {
   10681    131399763 :   tree ctor, idx, base;
   10682    131399763 :   poly_int64 offset, size, max_size;
   10683    131399763 :   tree tem;
   10684    131399763 :   bool reverse;
   10685              : 
   10686    131399763 :   if (TREE_THIS_VOLATILE (t))
   10687              :     return NULL_TREE;
   10688              : 
   10689    131078189 :   if (DECL_P (t))
   10690       304329 :     return get_symbol_constant_value (t);
   10691              : 
   10692    130773860 :   tem = fold_read_from_constant_string (t);
   10693    130773860 :   if (tem)
   10694              :     return tem;
   10695              : 
   10696    130771988 :   switch (TREE_CODE (t))
   10697              :     {
   10698     11007856 :     case ARRAY_REF:
   10699     11007856 :     case ARRAY_RANGE_REF:
   10700              :       /* Constant indexes are handled well by get_base_constructor.
   10701              :          Only special case variable offsets.
   10702              :          FIXME: This code can't handle nested references with variable indexes
   10703              :          (they will be handled only by iteration of ccp).  Perhaps we can bring
   10704              :          get_ref_base_and_extent here and make it use a valueize callback.  */
   10705     11007856 :       if (TREE_CODE (TREE_OPERAND (t, 1)) == SSA_NAME
   10706      6490530 :           && valueize
   10707      4365769 :           && (idx = (*valueize) (TREE_OPERAND (t, 1)))
   10708     15373625 :           && poly_int_tree_p (idx))
   10709              :         {
   10710      1740633 :           tree low_bound, unit_size;
   10711              : 
   10712              :           /* If the resulting bit-offset is constant, track it.  */
   10713      1740633 :           if ((low_bound = array_ref_low_bound (t),
   10714      1740633 :                poly_int_tree_p (low_bound))
   10715      1740633 :               && (unit_size = array_ref_element_size (t),
   10716      1740633 :                   tree_fits_uhwi_p (unit_size)))
   10717              :             {
   10718      1740633 :               poly_offset_int woffset
   10719      1740633 :                 = wi::sext (wi::to_poly_offset (idx)
   10720      3481266 :                             - wi::to_poly_offset (low_bound),
   10721      1740633 :                             TYPE_PRECISION (sizetype));
   10722      1740633 :               woffset *= tree_to_uhwi (unit_size);
   10723      1740633 :               woffset *= BITS_PER_UNIT;
   10724      1740633 :               if (woffset.to_shwi (&offset))
   10725              :                 {
   10726      1740514 :                   base = TREE_OPERAND (t, 0);
   10727      1740514 :                   ctor = get_base_constructor (base, &offset, valueize);
   10728              :                   /* Empty constructor.  Always fold to 0.  */
   10729      1740514 :                   if (ctor == error_mark_node)
   10730      1740514 :                     return build_zero_cst (TREE_TYPE (t));
   10731              :                   /* Out of bound array access.  Value is undefined,
   10732              :                      but don't fold.  */
   10733      1740433 :                   if (maybe_lt (offset, 0))
   10734              :                     return NULL_TREE;
   10735              :                   /* We cannot determine ctor.  */
   10736      1740014 :                   if (!ctor)
   10737              :                     return NULL_TREE;
   10738       181347 :                   return fold_ctor_reference (TREE_TYPE (t), ctor, offset,
   10739       181347 :                                               tree_to_uhwi (unit_size)
   10740       362694 :                                               * BITS_PER_UNIT,
   10741              :                                               base);
   10742              :                 }
   10743              :             }
   10744              :         }
   10745              :       /* Fallthru.  */
   10746              : 
   10747    124004336 :     case COMPONENT_REF:
   10748    124004336 :     case BIT_FIELD_REF:
   10749    124004336 :     case TARGET_MEM_REF:
   10750    124004336 :     case MEM_REF:
   10751    124004336 :       base = get_ref_base_and_extent (t, &offset, &size, &max_size, &reverse);
   10752    124004336 :       ctor = get_base_constructor (base, &offset, valueize);
   10753              : 
   10754              :       /* We cannot determine ctor.  */
   10755    124004336 :       if (!ctor)
   10756              :         return NULL_TREE;
   10757              :       /* Empty constructor.  Always fold to 0.  */
   10758      1223759 :       if (ctor == error_mark_node)
   10759         1233 :         return build_zero_cst (TREE_TYPE (t));
   10760              :       /* We do not know precise access.  */
   10761      1222526 :       if (!known_size_p (max_size) || maybe_ne (max_size, size))
   10762              :         return NULL_TREE;
   10763              :       /* Out of bound array access.  Value is undefined, but don't fold.  */
   10764       502935 :       if (maybe_lt (offset, 0))
   10765              :         return NULL_TREE;
   10766              :       /* Access with reverse storage order.  */
   10767       502554 :       if (reverse)
   10768              :         return NULL_TREE;
   10769              : 
   10770       502554 :       tem = fold_ctor_reference (TREE_TYPE (t), ctor, offset, size, base);
   10771       502554 :       if (tem)
   10772              :         return tem;
   10773              : 
   10774              :       /* For bit field reads try to read the representative and
   10775              :          adjust.  */
   10776       170652 :       if (TREE_CODE (t) == COMPONENT_REF
   10777         5153 :           && DECL_BIT_FIELD (TREE_OPERAND (t, 1))
   10778       170736 :           && DECL_BIT_FIELD_REPRESENTATIVE (TREE_OPERAND (t, 1)))
   10779              :         {
   10780           84 :           HOST_WIDE_INT csize, coffset;
   10781           84 :           tree field = TREE_OPERAND (t, 1);
   10782           84 :           tree repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
   10783          168 :           if (INTEGRAL_TYPE_P (TREE_TYPE (repr))
   10784           83 :               && size.is_constant (&csize)
   10785           83 :               && offset.is_constant (&coffset)
   10786           83 :               && (coffset % BITS_PER_UNIT != 0
   10787           81 :                   || csize % BITS_PER_UNIT != 0)
   10788           84 :               && BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN)
   10789              :             {
   10790           10 :               poly_int64 bitoffset;
   10791           10 :               poly_uint64 field_offset, repr_offset;
   10792           10 :               if (poly_int_tree_p (DECL_FIELD_OFFSET (field), &field_offset)
   10793           20 :                   && poly_int_tree_p (DECL_FIELD_OFFSET (repr), &repr_offset))
   10794           10 :                 bitoffset = (field_offset - repr_offset) * BITS_PER_UNIT;
   10795              :               else
   10796              :                 bitoffset = 0;
   10797           10 :               bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
   10798           10 :                             - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
   10799           10 :               HOST_WIDE_INT bitoff;
   10800           10 :               int diff = (TYPE_PRECISION (TREE_TYPE (repr))
   10801           10 :                           - TYPE_PRECISION (TREE_TYPE (field)));
   10802           10 :               if (bitoffset.is_constant (&bitoff)
   10803           10 :                   && bitoff >= 0
   10804           10 :                   && bitoff <= diff)
   10805              :                 {
   10806           10 :                   offset -= bitoff;
   10807           10 :                   size = tree_to_uhwi (DECL_SIZE (repr));
   10808              : 
   10809           20 :                   tem = fold_ctor_reference (TREE_TYPE (repr), ctor, offset,
   10810           10 :                                              size, base);
   10811           10 :                   if (tem && TREE_CODE (tem) == INTEGER_CST)
   10812              :                     {
   10813           10 :                       if (!BYTES_BIG_ENDIAN)
   10814           10 :                         tem = wide_int_to_tree (TREE_TYPE (field),
   10815           10 :                                                 wi::lrshift (wi::to_wide (tem),
   10816              :                                                              bitoff));
   10817              :                       else
   10818              :                         tem = wide_int_to_tree (TREE_TYPE (field),
   10819              :                                                 wi::lrshift (wi::to_wide (tem),
   10820              :                                                              diff - bitoff));
   10821           10 :                       return tem;
   10822              :                     }
   10823              :                 }
   10824              :             }
   10825              :         }
   10826              :       break;
   10827              : 
   10828      1854426 :     case REALPART_EXPR:
   10829      1854426 :     case IMAGPART_EXPR:
   10830      1854426 :       {
   10831      1854426 :         tree c = fold_const_aggregate_ref_1 (TREE_OPERAND (t, 0), valueize);
   10832      1854426 :         if (c && TREE_CODE (c) == COMPLEX_CST)
   10833         2786 :           return fold_build1_loc (EXPR_LOCATION (t),
   10834         5572 :                                   TREE_CODE (t), TREE_TYPE (t), c);
   10835              :         break;
   10836              :       }
   10837              : 
   10838              :     default:
   10839              :       break;
   10840              :     }
   10841              : 
   10842              :   return NULL_TREE;
   10843              : }
   10844              : 
   10845              : tree
   10846     63316726 : fold_const_aggregate_ref (tree t)
   10847              : {
   10848     63316726 :   return fold_const_aggregate_ref_1 (t, NULL);
   10849              : }
   10850              : 
   10851              : /* Lookup virtual method with index TOKEN in a virtual table V
   10852              :    at OFFSET.
   10853              :    Set CAN_REFER if non-NULL to false if method
   10854              :    is not referable or if the virtual table is ill-formed (such as rewritten
   10855              :    by non-C++ produced symbol). Otherwise just return NULL in that calse.  */
   10856              : 
   10857              : tree
   10858       284921 : gimple_get_virt_method_for_vtable (HOST_WIDE_INT token,
   10859              :                                    tree v,
   10860              :                                    unsigned HOST_WIDE_INT offset,
   10861              :                                    bool *can_refer)
   10862              : {
   10863       284921 :   tree vtable = v, init, fn;
   10864       284921 :   unsigned HOST_WIDE_INT size;
   10865       284921 :   unsigned HOST_WIDE_INT elt_size, access_index;
   10866       284921 :   tree domain_type;
   10867              : 
   10868       284921 :   if (can_refer)
   10869       284921 :     *can_refer = true;
   10870              : 
   10871              :   /* First of all double check we have virtual table.  */
   10872       284921 :   if (!VAR_P (v) || !DECL_VIRTUAL_P (v))
   10873              :     {
   10874              :       /* Pass down that we lost track of the target.  */
   10875            0 :       if (can_refer)
   10876            0 :         *can_refer = false;
   10877              :       return NULL_TREE;
   10878              :     }
   10879              : 
   10880       284921 :   init = ctor_for_folding (v);
   10881              : 
   10882              :   /* The virtual tables should always be born with constructors
   10883              :      and we always should assume that they are available for
   10884              :      folding.  At the moment we do not stream them in all cases,
   10885              :      but it should never happen that ctor seem unreachable.  */
   10886       284921 :   gcc_assert (init);
   10887       284921 :   if (init == error_mark_node)
   10888              :     {
   10889              :       /* Pass down that we lost track of the target.  */
   10890          213 :       if (can_refer)
   10891          213 :         *can_refer = false;
   10892              :       return NULL_TREE;
   10893              :     }
   10894       284708 :   gcc_checking_assert (TREE_CODE (TREE_TYPE (v)) == ARRAY_TYPE);
   10895       284708 :   size = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (v))));
   10896       284708 :   offset *= BITS_PER_UNIT;
   10897       284708 :   offset += token * size;
   10898              : 
   10899              :   /* Lookup the value in the constructor that is assumed to be array.
   10900              :      This is equivalent to
   10901              :      fn = fold_ctor_reference (TREE_TYPE (TREE_TYPE (v)), init,
   10902              :                                offset, size, NULL);
   10903              :      but in a constant time.  We expect that frontend produced a simple
   10904              :      array without indexed initializers.  */
   10905              : 
   10906       284708 :   gcc_checking_assert (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE);
   10907       284708 :   domain_type = TYPE_DOMAIN (TREE_TYPE (init));
   10908       284708 :   gcc_checking_assert (integer_zerop (TYPE_MIN_VALUE (domain_type)));
   10909       284708 :   elt_size = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (init))));
   10910              : 
   10911       284708 :   access_index = offset / BITS_PER_UNIT / elt_size;
   10912       284708 :   gcc_checking_assert (offset % (elt_size * BITS_PER_UNIT) == 0);
   10913              : 
   10914              :   /* This code makes an assumption that there are no
   10915              :      indexed fields produced by C++ FE, so we can directly index the array.  */
   10916       284708 :   if (access_index < CONSTRUCTOR_NELTS (init))
   10917              :     {
   10918       284707 :       fn = CONSTRUCTOR_ELT (init, access_index)->value;
   10919       284707 :       gcc_checking_assert (!CONSTRUCTOR_ELT (init, access_index)->index);
   10920       284707 :       STRIP_NOPS (fn);
   10921              :     }
   10922              :   else
   10923              :     fn = NULL;
   10924              : 
   10925              :   /* For type inconsistent program we may end up looking up virtual method
   10926              :      in virtual table that does not contain TOKEN entries.  We may overrun
   10927              :      the virtual table and pick up a constant or RTTI info pointer.
   10928              :      In any case the call is undefined.  */
   10929       284707 :   if (!fn
   10930       284707 :       || (TREE_CODE (fn) != ADDR_EXPR && TREE_CODE (fn) != FDESC_EXPR)
   10931       563605 :       || TREE_CODE (TREE_OPERAND (fn, 0)) != FUNCTION_DECL)
   10932         5810 :     fn = builtin_decl_unreachable ();
   10933              :   else
   10934              :     {
   10935       278898 :       fn = TREE_OPERAND (fn, 0);
   10936              : 
   10937              :       /* When cgraph node is missing and function is not public, we cannot
   10938              :          devirtualize.  This can happen in WHOPR when the actual method
   10939              :          ends up in other partition, because we found devirtualization
   10940              :          possibility too late.  */
   10941       278898 :       if (!can_refer_decl_in_current_unit_p (fn, vtable))
   10942              :         {
   10943        37309 :           if (can_refer)
   10944              :             {
   10945        37309 :               *can_refer = false;
   10946        37309 :               return fn;
   10947              :             }
   10948              :           return NULL_TREE;
   10949              :         }
   10950              :     }
   10951              : 
   10952              :   /* Make sure we create a cgraph node for functions we'll reference.
   10953              :      They can be non-existent if the reference comes from an entry
   10954              :      of an external vtable for example.  */
   10955       247399 :   cgraph_node::get_create (fn);
   10956              : 
   10957       247399 :   return fn;
   10958              : }
   10959              : 
   10960              : /* Return a declaration of a function which an OBJ_TYPE_REF references. TOKEN
   10961              :    is integer form of OBJ_TYPE_REF_TOKEN of the reference expression.
   10962              :    KNOWN_BINFO carries the binfo describing the true type of
   10963              :    OBJ_TYPE_REF_OBJECT(REF).
   10964              :    Set CAN_REFER if non-NULL to false if method
   10965              :    is not referable or if the virtual table is ill-formed (such as rewritten
   10966              :    by non-C++ produced symbol). Otherwise just return NULL in that calse.  */
   10967              : 
   10968              : tree
   10969       275826 : gimple_get_virt_method_for_binfo (HOST_WIDE_INT token, tree known_binfo,
   10970              :                                   bool *can_refer)
   10971              : {
   10972       275826 :   unsigned HOST_WIDE_INT offset;
   10973       275826 :   tree v;
   10974              : 
   10975       275826 :   v = BINFO_VTABLE (known_binfo);
   10976              :   /* If there is no virtual methods table, leave the OBJ_TYPE_REF alone.  */
   10977       275826 :   if (!v)
   10978              :     return NULL_TREE;
   10979              : 
   10980       275826 :   if (!vtable_pointer_value_to_vtable (v, &v, &offset))
   10981              :     {
   10982            0 :       if (can_refer)
   10983            0 :         *can_refer = false;
   10984              :       return NULL_TREE;
   10985              :     }
   10986       275826 :   return gimple_get_virt_method_for_vtable (token, v, offset, can_refer);
   10987              : }
   10988              : 
   10989              : /* Given a pointer value T, return a simplified version of an
   10990              :    indirection through T, or NULL_TREE if no simplification is
   10991              :    possible.  Note that the resulting type may be different from
   10992              :    the type pointed to in the sense that it is still compatible
   10993              :    from the langhooks point of view. */
   10994              : 
   10995              : tree
   10996      2528411 : gimple_fold_indirect_ref (tree t)
   10997              : {
   10998      2528411 :   tree ptype = TREE_TYPE (t), type = TREE_TYPE (ptype);
   10999      2528411 :   tree sub = t;
   11000      2528411 :   tree subtype;
   11001              : 
   11002      2528411 :   STRIP_NOPS (sub);
   11003      2528411 :   subtype = TREE_TYPE (sub);
   11004      2528411 :   if (!POINTER_TYPE_P (subtype)
   11005      2528411 :       || TYPE_REF_CAN_ALIAS_ALL (ptype))
   11006              :     return NULL_TREE;
   11007              : 
   11008      2526702 :   if (TREE_CODE (sub) == ADDR_EXPR)
   11009              :     {
   11010        97510 :       tree op = TREE_OPERAND (sub, 0);
   11011        97510 :       tree optype = TREE_TYPE (op);
   11012              :       /* *&p => p */
   11013        97510 :       if (useless_type_conversion_p (type, optype))
   11014              :         return op;
   11015              : 
   11016              :       /* *(foo *)&fooarray => fooarray[0] */
   11017         1021 :       if (TREE_CODE (optype) == ARRAY_TYPE
   11018          335 :           && TREE_CODE (TYPE_SIZE (TREE_TYPE (optype))) == INTEGER_CST
   11019         1356 :           && useless_type_conversion_p (type, TREE_TYPE (optype)))
   11020              :        {
   11021           54 :          tree type_domain = TYPE_DOMAIN (optype);
   11022           54 :          tree min_val = size_zero_node;
   11023           54 :          if (type_domain && TYPE_MIN_VALUE (type_domain))
   11024           54 :            min_val = TYPE_MIN_VALUE (type_domain);
   11025           54 :          if (TREE_CODE (min_val) == INTEGER_CST)
   11026           54 :            return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
   11027              :        }
   11028              :       /* *(foo *)&complexfoo => __real__ complexfoo */
   11029          967 :       else if (TREE_CODE (optype) == COMPLEX_TYPE
   11030          967 :                && useless_type_conversion_p (type, TREE_TYPE (optype)))
   11031            4 :         return fold_build1 (REALPART_EXPR, type, op);
   11032              :       /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
   11033          963 :       else if (TREE_CODE (optype) == VECTOR_TYPE
   11034          963 :                && useless_type_conversion_p (type, TREE_TYPE (optype)))
   11035              :         {
   11036           26 :           tree part_width = TYPE_SIZE (type);
   11037           26 :           tree index = bitsize_int (0);
   11038           26 :           return fold_build3 (BIT_FIELD_REF, type, op, part_width, index);
   11039              :         }
   11040              :     }
   11041              : 
   11042              :   /* *(p + CST) -> ...  */
   11043      2430129 :   if (TREE_CODE (sub) == POINTER_PLUS_EXPR
   11044      2430129 :       && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
   11045              :     {
   11046        33922 :       tree addr = TREE_OPERAND (sub, 0);
   11047        33922 :       tree off = TREE_OPERAND (sub, 1);
   11048        33922 :       tree addrtype;
   11049              : 
   11050        33922 :       STRIP_NOPS (addr);
   11051        33922 :       addrtype = TREE_TYPE (addr);
   11052              : 
   11053              :       /* ((foo*)&vectorfoo)[1] -> BIT_FIELD_REF<vectorfoo,...> */
   11054        33922 :       if (TREE_CODE (addr) == ADDR_EXPR
   11055           92 :           && TREE_CODE (TREE_TYPE (addrtype)) == VECTOR_TYPE
   11056           39 :           && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype)))
   11057        33950 :           && tree_fits_uhwi_p (off))
   11058              :         {
   11059           28 :           unsigned HOST_WIDE_INT offset = tree_to_uhwi (off);
   11060           28 :           tree part_width = TYPE_SIZE (type);
   11061           28 :           unsigned HOST_WIDE_INT part_widthi
   11062           28 :             = tree_to_shwi (part_width) / BITS_PER_UNIT;
   11063           28 :           unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
   11064           28 :           tree index = bitsize_int (indexi);
   11065           28 :           if (known_lt (offset / part_widthi,
   11066              :                         TYPE_VECTOR_SUBPARTS (TREE_TYPE (addrtype))))
   11067           28 :             return fold_build3 (BIT_FIELD_REF, type, TREE_OPERAND (addr, 0),
   11068              :                                 part_width, index);
   11069              :         }
   11070              : 
   11071              :       /* ((foo*)&complexfoo)[1] -> __imag__ complexfoo */
   11072        33894 :       if (TREE_CODE (addr) == ADDR_EXPR
   11073           64 :           && TREE_CODE (TREE_TYPE (addrtype)) == COMPLEX_TYPE
   11074        33895 :           && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype))))
   11075              :         {
   11076            1 :           tree size = TYPE_SIZE_UNIT (type);
   11077            1 :           if (tree_int_cst_equal (size, off))
   11078            1 :             return fold_build1 (IMAGPART_EXPR, type, TREE_OPERAND (addr, 0));
   11079              :         }
   11080              : 
   11081              :       /* *(p + CST) -> MEM_REF <p, CST>.  */
   11082        33893 :       if (TREE_CODE (addr) != ADDR_EXPR
   11083        33893 :           || DECL_P (TREE_OPERAND (addr, 0)))
   11084        33875 :         return fold_build2 (MEM_REF, type,
   11085              :                             addr,
   11086              :                             wide_int_to_tree (ptype, wi::to_wide (off)));
   11087              :     }
   11088              : 
   11089              :   /* *(foo *)fooarrptr => (*fooarrptr)[0] */
   11090      2396225 :   if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
   11091         2546 :       && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (subtype)))) == INTEGER_CST
   11092      2398741 :       && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (subtype))))
   11093              :     {
   11094            1 :       tree type_domain;
   11095            1 :       tree min_val = size_zero_node;
   11096            1 :       tree osub = sub;
   11097            1 :       sub = gimple_fold_indirect_ref (sub);
   11098            1 :       if (! sub)
   11099            1 :         sub = build1 (INDIRECT_REF, TREE_TYPE (subtype), osub);
   11100            1 :       type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
   11101            1 :       if (type_domain && TYPE_MIN_VALUE (type_domain))
   11102            1 :         min_val = TYPE_MIN_VALUE (type_domain);
   11103            1 :       if (TREE_CODE (min_val) == INTEGER_CST)
   11104            1 :         return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
   11105              :     }
   11106              : 
   11107              :   return NULL_TREE;
   11108              : }
   11109              : 
   11110              : /* Return true if CODE is an operation that when operating on signed
   11111              :    integer types involves undefined behavior on overflow and the
   11112              :    operation can be expressed with unsigned arithmetic.  */
   11113              : 
   11114              : bool
   11115       635331 : arith_code_with_undefined_signed_overflow (tree_code code)
   11116              : {
   11117       635331 :   switch (code)
   11118              :     {
   11119              :     case ABS_EXPR:
   11120              :     case PLUS_EXPR:
   11121              :     case MINUS_EXPR:
   11122              :     case MULT_EXPR:
   11123              :     case NEGATE_EXPR:
   11124              :     case POINTER_PLUS_EXPR:
   11125              :       return true;
   11126       282743 :     default:
   11127       282743 :       return false;
   11128              :     }
   11129              : }
   11130              : 
   11131              : /* Return true if STMT has an operation that operates on a signed
   11132              :    integer types involves undefined behavior on overflow and the
   11133              :    operation can be expressed with unsigned arithmetic.
   11134              :    Also returns true if STMT is a VCE that needs to be rewritten
   11135              :    if moved to be executed unconditionally, or a CLZ or CTZ call
   11136              :    with no defined result for a zero argument.  STMT may be a call
   11137              :    only in that last case, otherwise it must be an assignment.
   11138              :    SHIFT_COUNT additionally asks for a shift or rotate whose count may be
   11139              :    out of range; rewrite_to_defined_unconditional reduces such a count.  */
   11140              : 
   11141              : bool
   11142      1666783 : gimple_needing_rewrite_undefined (gimple *stmt, bool shift_count)
   11143              : {
   11144      1666783 :   if (gcall *call = dyn_cast <gcall *> (stmt))
   11145         2576 :     switch (gimple_call_combined_fn (call))
   11146              :       {
   11147          328 :       CASE_CFN_CLZ:
   11148          328 :       CASE_CFN_CTZ:
   11149          328 :         return (gimple_call_num_args (call) == 1
   11150          328 :                 && INTEGRAL_TYPE_P (TREE_TYPE (gimple_call_arg (call, 0))));
   11151              :       default:
   11152              :         return false;
   11153              :       }
   11154      1664207 :   gassign *as = dyn_cast<gassign *> (stmt);
   11155      1406042 :   if (!as)
   11156              :     return false;
   11157      1406042 :   tree lhs = gimple_assign_lhs (as);
   11158      1406042 :   if (!lhs)
   11159              :     return false;
   11160      1406042 :   tree lhs_type = TREE_TYPE (lhs);
   11161      1406042 :   if (!INTEGRAL_TYPE_P (lhs_type)
   11162       159444 :       && !POINTER_TYPE_P (lhs_type))
   11163              :     return false;
   11164      1356813 :   tree rhs = gimple_assign_rhs1 (as);
   11165              :   /* Boolean loads need special handling as they are treated as a full MODE load
   11166              :      and don't mask off the bits for the precision.  */
   11167      1356813 :   if (gimple_assign_load_p (as)
   11168              :       /* Booleans are the integral type which has this non-masking issue. */
   11169       170366 :       && TREE_CODE (lhs_type) == BOOLEAN_TYPE
   11170              :       /* Only non mode precision booleans are need the masking.  */
   11171          584 :       && !type_has_mode_precision_p (lhs_type)
   11172              :       /* BFR should be the correct thing and just grab the precision.  */
   11173          584 :       && TREE_CODE (rhs) != BIT_FIELD_REF
   11174              :       /* Bit-fields loads don't need a rewrite as the masking
   11175              :          happens for them.  */
   11176      1357397 :       && (TREE_CODE (rhs) != COMPONENT_REF
   11177          199 :           || !DECL_BIT_FIELD (TREE_OPERAND (rhs, 1))))
   11178              :     return true;
   11179              :   /* VCE from integral types to a integral types but with
   11180              :      a smaller precision need to be changed into casts
   11181              :      to be well defined. */
   11182      1356229 :   if (gimple_assign_rhs_code (as) == VIEW_CONVERT_EXPR
   11183          220 :       && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (rhs, 0)))
   11184          173 :       && is_gimple_val (TREE_OPERAND (rhs, 0))
   11185      1356402 :       && TYPE_PRECISION (lhs_type)
   11186          173 :           < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (rhs, 0))))
   11187              :     return true;
   11188              :   /* A shift or rotate by a count that may be out of range; the count can
   11189              :      be reduced into range.  */
   11190      1356062 :   enum tree_code code = gimple_assign_rhs_code (stmt);
   11191      1356062 :   if (shift_count
   11192       817532 :       && (code == LSHIFT_EXPR
   11193              :           || code == RSHIFT_EXPR
   11194              :           || code == LROTATE_EXPR
   11195       817532 :           || code == RROTATE_EXPR)
   11196        61144 :       && INTEGRAL_TYPE_P (lhs_type)
   11197      1417206 :       && (TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST
   11198        61144 :           || wi::geu_p (wi::to_wide (gimple_assign_rhs2 (stmt)),
   11199        16848 :                         TYPE_PRECISION (lhs_type))))
   11200              :     return true;
   11201      1311762 :   if (!TYPE_OVERFLOW_UNDEFINED (lhs_type))
   11202              :     return false;
   11203       507120 :   if (!arith_code_with_undefined_signed_overflow (code))
   11204       261724 :     return false;
   11205              :   return true;
   11206              : }
   11207              : 
   11208              : /* Rewrite STMT, an assignment with a signed integer or pointer arithmetic
   11209              :    operation that can be transformed to unsigned arithmetic by converting
   11210              :    its operand, carrying out the operation in the corresponding unsigned
   11211              :    type and converting the result back to the original type.
   11212              :    STMT may also be a one-argument CLZ or CTZ call, which is replaced by
   11213              :    the two-argument internal function whose result for a zero argument is
   11214              :    the value defined by the target, or the precision of the argument if
   11215              :    the target defines none.
   11216              : 
   11217              :    If IN_PLACE is true, *GSI points to STMT, adjust the stmt in place and
   11218              :    return NULL.
   11219              :    Otherwise returns a sequence of statements that replace STMT and also
   11220              :    contain a modified form of STMT itself.  */
   11221              : 
   11222              : static gimple_seq
   11223        70728 : rewrite_to_defined_unconditional (gimple_stmt_iterator *gsi, gimple *stmt,
   11224              :                                   bool in_place)
   11225              : {
   11226        70728 :   gcc_assert (gimple_needing_rewrite_undefined (stmt, true));
   11227        70728 :   if (dump_file && (dump_flags & TDF_DETAILS))
   11228              :     {
   11229           59 :       fprintf (dump_file, "rewriting stmt for being unconditional defined");
   11230           59 :       print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
   11231              :     }
   11232        70728 :   gimple_seq stmts = NULL;
   11233              : 
   11234              :   /* Give a CLZ or CTZ call a defined result for a zero argument.  */
   11235        70728 :   if (gcall *call = dyn_cast <gcall *> (stmt))
   11236              :     {
   11237          122 :       gcc_checking_assert (gimple_call_num_args (call) == 1);
   11238          122 :       tree arg = gimple_call_arg (call, 0);
   11239          122 :       gcc_checking_assert (INTEGRAL_TYPE_P (TREE_TYPE (arg)));
   11240          122 :       scalar_int_mode mode;
   11241          122 :       int val = 0, defined = 0;
   11242          122 :       internal_fn ifn;
   11243          122 :       switch (gimple_call_combined_fn (call))
   11244              :         {
   11245              :         CASE_CFN_CLZ:
   11246              :           ifn = IFN_CLZ;
   11247              :           break;
   11248           69 :         CASE_CFN_CTZ:
   11249           69 :           ifn = IFN_CTZ;
   11250           69 :           break;
   11251            0 :         default:
   11252            0 :           gcc_unreachable ();
   11253              :         }
   11254              :       /* Large _BitInt types have no scalar integer mode and no target
   11255              :          defined value.  */
   11256          122 :       if (is_a <scalar_int_mode> (TYPE_MODE (TREE_TYPE (arg)), &mode))
   11257          244 :         defined = (ifn == IFN_CLZ
   11258          175 :                    ? CLZ_DEFINED_VALUE_AT_ZERO (mode, val)
   11259          138 :                    : CTZ_DEFINED_VALUE_AT_ZERO (mode, val));
   11260          122 :       if (defined != 2)
   11261           74 :         val = TYPE_PRECISION (TREE_TYPE (arg));
   11262          122 :       tree zero_val = build_int_cst (integer_type_node, val);
   11263          122 :       gcall *new_call = gimple_build_call_internal (ifn, 2, arg, zero_val);
   11264          122 :       gimple_call_set_lhs (new_call, gimple_call_lhs (call));
   11265          122 :       gimple_set_location (new_call, gimple_location (call));
   11266          122 :       if (in_place)
   11267              :         {
   11268          122 :           gsi_replace (gsi, new_call, true);
   11269          122 :           return NULL;
   11270              :         }
   11271            0 :       gimple_seq_add_stmt (&stmts, new_call);
   11272            0 :       return stmts;
   11273              :     }
   11274              : 
   11275        70606 :   tree lhs = gimple_assign_lhs (stmt);
   11276              : 
   11277              :   /* Boolean loads need to be rewritten to be a load from the same mode
   11278              :      and then a cast to the other type so the other bits are masked off
   11279              :      correctly since the load was done conditionally. It is similar to the VCE
   11280              :      case below.  */
   11281        70606 :   if (gimple_assign_load_p (stmt)
   11282        70606 :       && TREE_CODE (TREE_TYPE (lhs)) == BOOLEAN_TYPE)
   11283              :     {
   11284          146 :       tree rhs = gimple_assign_rhs1 (stmt);
   11285              : 
   11286              :       /* Double check that gimple_needing_rewrite_undefined was called.  */
   11287              :       /* Bit-fields loads will do the masking so don't need the rewriting.  */
   11288          146 :       gcc_assert (TREE_CODE (rhs) != COMPONENT_REF
   11289              :                   || !DECL_BIT_FIELD (TREE_OPERAND (rhs, 1)));
   11290              :       /* BFR is like a bit field load and will do the correct thing.  */
   11291          146 :       gcc_assert (TREE_CODE (lhs) != BIT_FIELD_REF);
   11292              :       /* Complex boolean types are not valid so REAL/IMAG part will
   11293              :          never show up. */
   11294          146 :       gcc_assert (TREE_CODE (rhs) != REALPART_EXPR
   11295              :                   && TREE_CODE (lhs) != IMAGPART_EXPR);
   11296              : 
   11297          146 :       auto bits = GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (rhs)));
   11298          146 :       tree new_type = build_nonstandard_integer_type (bits, true);
   11299          146 :       location_t loc = gimple_location (stmt);
   11300          146 :       tree mem_ref = fold_build1_loc (loc, VIEW_CONVERT_EXPR, new_type, rhs);
   11301              :       /* Replace the original load with a new load and a new lhs. */
   11302          146 :       tree new_lhs = make_ssa_name (new_type);
   11303          146 :       gimple_assign_set_rhs1 (stmt, mem_ref);
   11304          146 :       gimple_assign_set_lhs (stmt, new_lhs);
   11305              : 
   11306          146 :       if (in_place)
   11307           49 :           update_stmt (stmt);
   11308              :       else
   11309              :         {
   11310           97 :           gimple_set_modified (stmt, true);
   11311           97 :           gimple_seq_add_stmt (&stmts, stmt);
   11312              :         }
   11313              : 
   11314              :       /* Build the conversion statement.  */
   11315          146 :       gimple *cvt = gimple_build_assign (lhs, NOP_EXPR, new_lhs);
   11316          146 :       if (in_place)
   11317              :         {
   11318           49 :           gsi_insert_after (gsi, cvt, GSI_SAME_STMT);
   11319           49 :           update_stmt (stmt);
   11320              :         }
   11321              :       else
   11322           97 :         gimple_seq_add_stmt (&stmts, cvt);
   11323          146 :       return stmts;
   11324              :     }
   11325              : 
   11326              :   /* VCE from integral types to another integral types but with
   11327              :      smaller precisions need to be changed into casts
   11328              :      to be well defined. */
   11329        70460 :   if (gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR)
   11330              :     {
   11331           60 :       tree rhs = gimple_assign_rhs1 (stmt);
   11332           60 :       tree new_rhs = TREE_OPERAND (rhs, 0);
   11333           60 :       gcc_assert (TYPE_PRECISION (TREE_TYPE (rhs))
   11334              :                   < TYPE_PRECISION (TREE_TYPE (new_rhs)));
   11335           60 :       gcc_assert (is_gimple_val (new_rhs));
   11336           60 :       gimple_assign_set_rhs_code (stmt, NOP_EXPR);
   11337           60 :       gimple_assign_set_rhs1 (stmt, new_rhs);
   11338           60 :       if (in_place)
   11339           51 :           update_stmt (stmt);
   11340              :       else
   11341              :         {
   11342            9 :           gimple_set_modified (stmt, true);
   11343            9 :           gimple_seq_add_stmt (&stmts, stmt);
   11344              :         }
   11345           60 :       return stmts;
   11346              :     }
   11347              :   /* Reduce a shift or rotate count into range.  The mask keeps the count's
   11348              :      own type where that holds PREC - 1, so that a SHIFT_COUNT_TRUNCATED
   11349              :      target folds it away; MIN needs an unsigned count so a negative one
   11350              :      clamps.  */
   11351        70400 :   enum tree_code code = gimple_assign_rhs_code (stmt);
   11352        70400 :   if (code == LSHIFT_EXPR
   11353              :       || code == RSHIFT_EXPR
   11354              :       || code == LROTATE_EXPR
   11355        70400 :       || code == RROTATE_EXPR)
   11356              :     {
   11357          141 :       unsigned prec = TYPE_PRECISION (TREE_TYPE (lhs));
   11358          141 :       bool pow2 = pow2p_hwi (prec);
   11359          141 :       tree count = gimple_assign_rhs2 (stmt);
   11360          141 :       tree type = TREE_TYPE (count);
   11361          281 :       if (!pow2 || wi::ltu_p (wi::max_value (type), prec - 1))
   11362            1 :         type = unsigned_type_node;
   11363          141 :       count = gimple_convert (&stmts, type, count);
   11364          142 :       count = gimple_build (&stmts, pow2 ? BIT_AND_EXPR : MIN_EXPR, type,
   11365          141 :                             count, build_int_cst (type, prec - 1));
   11366          141 :       gimple_assign_set_rhs2 (stmt, count);
   11367          141 :       gimple_set_modified (stmt, true);
   11368          141 :       if (in_place)
   11369              :         {
   11370            0 :           gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
   11371            0 :           update_stmt (stmt);
   11372            0 :           return NULL;
   11373              :         }
   11374          141 :       gimple_seq_add_stmt (&stmts, stmt);
   11375          141 :       return stmts;
   11376              :     }
   11377        70259 :   tree type = unsigned_type_for (TREE_TYPE (lhs));
   11378        70259 :   if (gimple_assign_rhs_code (stmt) == ABS_EXPR)
   11379           21 :     gimple_assign_set_rhs_code (stmt, ABSU_EXPR);
   11380              :   else
   11381       210176 :     for (unsigned i = 1; i < gimple_num_ops (stmt); ++i)
   11382              :       {
   11383       139938 :         tree op = gimple_op (stmt, i);
   11384       139938 :         op = gimple_convert (&stmts, type, op);
   11385       139938 :         gimple_set_op (stmt, i, op);
   11386              :       }
   11387        70259 :   gimple_assign_set_lhs (stmt, make_ssa_name (type, stmt));
   11388        70259 :   if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
   11389         8883 :     gimple_assign_set_rhs_code (stmt, PLUS_EXPR);
   11390        70259 :   gimple_set_modified (stmt, true);
   11391        70259 :   if (in_place)
   11392              :     {
   11393        49659 :       if (stmts)
   11394        49397 :         gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
   11395        49659 :       stmts = NULL;
   11396              :     }
   11397              :   else
   11398        20600 :     gimple_seq_add_stmt (&stmts, stmt);
   11399        70259 :   gimple *cvt = gimple_build_assign (lhs, NOP_EXPR, gimple_assign_lhs (stmt));
   11400        70259 :   if (in_place)
   11401              :     {
   11402        49659 :       gsi_insert_after (gsi, cvt, GSI_SAME_STMT);
   11403        49659 :       update_stmt (stmt);
   11404              :     }
   11405              :   else
   11406        20600 :     gimple_seq_add_stmt (&stmts, cvt);
   11407              : 
   11408        70259 :   return stmts;
   11409              : }
   11410              : 
   11411              : void
   11412        49881 : rewrite_to_defined_unconditional (gimple_stmt_iterator *gsi)
   11413              : {
   11414        49881 :   rewrite_to_defined_unconditional (gsi, gsi_stmt (*gsi), true);
   11415        49881 : }
   11416              : 
   11417              : gimple_seq
   11418        20847 : rewrite_to_defined_unconditional (gimple *stmt)
   11419              : {
   11420        20847 :   return rewrite_to_defined_unconditional (nullptr, stmt, false);
   11421              : }
   11422              : 
   11423              : /* The valueization hook we use for the gimple_build API simplification.
   11424              :    This makes us match fold_buildN behavior by only combining with
   11425              :    statements in the sequence(s) we are currently building.  */
   11426              : 
   11427              : static tree
   11428     22739942 : gimple_build_valueize (tree op)
   11429              : {
   11430     22739942 :   if (gimple_bb (SSA_NAME_DEF_STMT (op)) == NULL)
   11431      5097349 :     return op;
   11432              :   return NULL_TREE;
   11433              : }
   11434              : 
   11435              : /* Helper for gimple_build to perform the final insertion of stmts on SEQ.  */
   11436              : 
   11437              : static inline void
   11438      1423185 : gimple_build_insert_seq (gimple_stmt_iterator *gsi,
   11439              :                          bool before, gsi_iterator_update update,
   11440              :                          gimple_seq seq)
   11441              : {
   11442      1423185 :   if (before)
   11443              :     {
   11444        98277 :       if (gsi->bb)
   11445        98277 :         gsi_insert_seq_before (gsi, seq, update);
   11446              :       else
   11447            0 :         gsi_insert_seq_before_without_update (gsi, seq, update);
   11448              :     }
   11449              :   else
   11450              :     {
   11451      1324908 :       if (gsi->bb)
   11452          193 :         gsi_insert_seq_after (gsi, seq, update);
   11453              :       else
   11454      1324715 :         gsi_insert_seq_after_without_update (gsi, seq, update);
   11455              :     }
   11456      1423185 : }
   11457              : 
   11458              : /* Build the expression CODE OP0 of type TYPE with location LOC,
   11459              :    simplifying it first if possible.  Returns the built
   11460              :    expression value and inserts statements possibly defining it
   11461              :    before GSI if BEFORE is true or after GSI if false and advance
   11462              :    the iterator accordingly.
   11463              :    If gsi refers to a basic block simplifying is allowed to look
   11464              :    at all SSA defs while when it does not it is restricted to
   11465              :    SSA defs that are not associated with a basic block yet,
   11466              :    indicating they belong to the currently building sequence.  */
   11467              : 
   11468              : tree
   11469       370209 : gimple_build (gimple_stmt_iterator *gsi,
   11470              :               bool before, gsi_iterator_update update,
   11471              :               location_t loc, enum tree_code code, tree type, tree op0)
   11472              : {
   11473       370209 :   gimple_seq seq = NULL;
   11474       370209 :   tree res
   11475       370209 :     = gimple_simplify (code, type, op0, &seq,
   11476       370209 :                        gsi->bb ? follow_all_ssa_edges : gimple_build_valueize);
   11477       370209 :   if (!res)
   11478              :     {
   11479       317019 :       res = make_ssa_name (type);
   11480       317019 :       gimple *stmt;
   11481       317019 :       if (code == REALPART_EXPR
   11482              :           || code == IMAGPART_EXPR
   11483       317019 :           || code == VIEW_CONVERT_EXPR)
   11484        12184 :         stmt = gimple_build_assign (res, code, build1 (code, type, op0));
   11485              :       else
   11486       304835 :         stmt = gimple_build_assign (res, code, op0);
   11487       317019 :       gimple_set_location (stmt, loc);
   11488       317019 :       gimple_seq_add_stmt_without_update (&seq, stmt);
   11489              :     }
   11490       370209 :   gimple_build_insert_seq (gsi, before, update, seq);
   11491       370209 :   return res;
   11492              : }
   11493              : 
   11494              : /* Build the expression OP0 CODE OP1 of type TYPE with location LOC,
   11495              :    simplifying it first if possible.  Returns the built
   11496              :    expression value inserting any new statements at GSI honoring BEFORE
   11497              :    and UPDATE.  */
   11498              : 
   11499              : tree
   11500       837135 : gimple_build (gimple_stmt_iterator *gsi,
   11501              :               bool before, gsi_iterator_update update,
   11502              :               location_t loc, enum tree_code code, tree type,
   11503              :               tree op0, tree op1)
   11504              : {
   11505       837135 :   gimple_seq seq = NULL;
   11506       837135 :   tree res
   11507       837135 :     = gimple_simplify (code, type, op0, op1, &seq,
   11508       837135 :                        gsi->bb ? follow_all_ssa_edges : gimple_build_valueize);
   11509       837135 :   if (!res)
   11510              :     {
   11511       736870 :       res = make_ssa_name (type);
   11512       736870 :       gimple *stmt = gimple_build_assign (res, code, op0, op1);
   11513       736870 :       gimple_set_location (stmt, loc);
   11514       736870 :       gimple_seq_add_stmt_without_update (&seq, stmt);
   11515              :     }
   11516       837135 :   gimple_build_insert_seq (gsi, before, update, seq);
   11517       837135 :   return res;
   11518              : }
   11519              : 
   11520              : /* Build the expression (CODE OP0 OP1 OP2) of type TYPE with location LOC,
   11521              :    simplifying it first if possible.  Returns the built
   11522              :    expression value inserting any new statements at GSI honoring BEFORE
   11523              :    and UPDATE.  */
   11524              : 
   11525              : tree
   11526        48913 : gimple_build (gimple_stmt_iterator *gsi,
   11527              :               bool before, gsi_iterator_update update,
   11528              :               location_t loc, enum tree_code code, tree type,
   11529              :               tree op0, tree op1, tree op2)
   11530              : {
   11531              : 
   11532        48913 :   gimple_seq seq = NULL;
   11533        48913 :   tree res
   11534        48913 :     = gimple_simplify (code, type, op0, op1, op2, &seq,
   11535        48913 :                        gsi->bb ? follow_all_ssa_edges : gimple_build_valueize);
   11536        48913 :   if (!res)
   11537              :     {
   11538        35167 :       res = make_ssa_name (type);
   11539        35167 :       gimple *stmt;
   11540        35167 :       if (code == BIT_FIELD_REF)
   11541        27623 :         stmt = gimple_build_assign (res, code,
   11542              :                                     build3 (code, type, op0, op1, op2));
   11543              :       else
   11544         7544 :         stmt = gimple_build_assign (res, code, op0, op1, op2);
   11545        35167 :       gimple_set_location (stmt, loc);
   11546        35167 :       gimple_seq_add_stmt_without_update (&seq, stmt);
   11547              :     }
   11548        48913 :   gimple_build_insert_seq (gsi, before, update, seq);
   11549        48913 :   return res;
   11550              : }
   11551              : 
   11552              : /* Build the call FN () with a result of type TYPE (or no result if TYPE is
   11553              :    void) with a location LOC.  Returns the built expression value (or NULL_TREE
   11554              :    if TYPE is void) inserting any new statements at GSI honoring BEFORE
   11555              :    and UPDATE.  */
   11556              : 
   11557              : tree
   11558            0 : gimple_build (gimple_stmt_iterator *gsi,
   11559              :               bool before, gsi_iterator_update update,
   11560              :               location_t loc, combined_fn fn, tree type)
   11561              : {
   11562            0 :   tree res = NULL_TREE;
   11563            0 :   gimple_seq seq = NULL;
   11564            0 :   gcall *stmt;
   11565            0 :   if (internal_fn_p (fn))
   11566            0 :     stmt = gimple_build_call_internal (as_internal_fn (fn), 0);
   11567              :   else
   11568              :     {
   11569            0 :       tree decl = builtin_decl_implicit (as_builtin_fn (fn));
   11570            0 :       stmt = gimple_build_call (decl, 0);
   11571              :     }
   11572            0 :   if (!VOID_TYPE_P (type))
   11573              :     {
   11574            0 :       res = make_ssa_name (type);
   11575            0 :       gimple_call_set_lhs (stmt, res);
   11576              :     }
   11577            0 :   gimple_set_location (stmt, loc);
   11578            0 :   gimple_seq_add_stmt_without_update (&seq, stmt);
   11579            0 :   gimple_build_insert_seq (gsi, before, update, seq);
   11580            0 :   return res;
   11581              : }
   11582              : 
   11583              : /* Build the call FN (ARG0) with a result of type TYPE
   11584              :    (or no result if TYPE is void) with location LOC,
   11585              :    simplifying it first if possible.  Returns the built
   11586              :    expression value (or NULL_TREE if TYPE is void) inserting any new
   11587              :    statements at GSI honoring BEFORE and UPDATE.  */
   11588              : 
   11589              : tree
   11590        22050 : gimple_build (gimple_stmt_iterator *gsi,
   11591              :               bool before, gsi_iterator_update update,
   11592              :               location_t loc, combined_fn fn,
   11593              :               tree type, tree arg0)
   11594              : {
   11595        22050 :   gimple_seq seq = NULL;
   11596        22050 :   tree res = gimple_simplify (fn, type, arg0, &seq, gimple_build_valueize);
   11597        22050 :   if (!res)
   11598              :     {
   11599        22050 :       gcall *stmt;
   11600        22050 :       if (internal_fn_p (fn))
   11601        21674 :         stmt = gimple_build_call_internal (as_internal_fn (fn), 1, arg0);
   11602              :       else
   11603              :         {
   11604          376 :           tree decl = builtin_decl_implicit (as_builtin_fn (fn));
   11605          376 :           stmt = gimple_build_call (decl, 1, arg0);
   11606              :         }
   11607        22050 :       if (!VOID_TYPE_P (type))
   11608              :         {
   11609        21674 :           res = make_ssa_name (type);
   11610        21674 :           gimple_call_set_lhs (stmt, res);
   11611              :         }
   11612        22050 :       gimple_set_location (stmt, loc);
   11613        22050 :       gimple_seq_add_stmt_without_update (&seq, stmt);
   11614              :     }
   11615        22050 :   gimple_build_insert_seq  (gsi, before, update, seq);
   11616        22050 :   return res;
   11617              : }
   11618              : 
   11619              : /* Build the call FN (ARG0, ARG1) with a result of type TYPE
   11620              :    (or no result if TYPE is void) with location LOC,
   11621              :    simplifying it first if possible.  Returns the built
   11622              :    expression value (or NULL_TREE if TYPE is void) inserting any new
   11623              :    statements at GSI honoring BEFORE and UPDATE.  */
   11624              : 
   11625              : tree
   11626          138 : gimple_build (gimple_stmt_iterator *gsi,
   11627              :               bool before, gsi_iterator_update update,
   11628              :               location_t loc, combined_fn fn,
   11629              :               tree type, tree arg0, tree arg1)
   11630              : {
   11631          138 :   gimple_seq seq = NULL;
   11632          138 :   tree res = gimple_simplify (fn, type, arg0, arg1, &seq,
   11633              :                               gimple_build_valueize);
   11634          138 :   if (!res)
   11635              :     {
   11636          138 :       gcall *stmt;
   11637          138 :       if (internal_fn_p (fn))
   11638          138 :         stmt = gimple_build_call_internal (as_internal_fn (fn), 2, arg0, arg1);
   11639              :       else
   11640              :         {
   11641            0 :           tree decl = builtin_decl_implicit (as_builtin_fn (fn));
   11642            0 :           stmt = gimple_build_call (decl, 2, arg0, arg1);
   11643              :         }
   11644          138 :       if (!VOID_TYPE_P (type))
   11645              :         {
   11646          138 :           res = make_ssa_name (type);
   11647          138 :           gimple_call_set_lhs (stmt, res);
   11648              :         }
   11649          138 :       gimple_set_location (stmt, loc);
   11650          138 :       gimple_seq_add_stmt_without_update (&seq, stmt);
   11651              :     }
   11652          138 :   gimple_build_insert_seq (gsi, before, update, seq);
   11653          138 :   return res;
   11654              : }
   11655              : 
   11656              : /* Build the call FN (ARG0, ARG1, ARG2) with a result of type TYPE
   11657              :    (or no result if TYPE is void) with location LOC,
   11658              :    simplifying it first if possible.  Returns the built
   11659              :    expression value (or NULL_TREE if TYPE is void) inserting any new
   11660              :    statements at GSI honoring BEFORE and UPDATE.  */
   11661              : 
   11662              : tree
   11663            0 : gimple_build (gimple_stmt_iterator *gsi,
   11664              :               bool before, gsi_iterator_update update,
   11665              :               location_t loc, combined_fn fn,
   11666              :               tree type, tree arg0, tree arg1, tree arg2)
   11667              : {
   11668            0 :   gimple_seq seq = NULL;
   11669            0 :   tree res = gimple_simplify (fn, type, arg0, arg1, arg2,
   11670              :                               &seq, gimple_build_valueize);
   11671            0 :   if (!res)
   11672              :     {
   11673            0 :       gcall *stmt;
   11674            0 :       if (internal_fn_p (fn))
   11675            0 :         stmt = gimple_build_call_internal (as_internal_fn (fn),
   11676              :                                            3, arg0, arg1, arg2);
   11677              :       else
   11678              :         {
   11679            0 :           tree decl = builtin_decl_implicit (as_builtin_fn (fn));
   11680            0 :           stmt = gimple_build_call (decl, 3, arg0, arg1, arg2);
   11681              :         }
   11682            0 :       if (!VOID_TYPE_P (type))
   11683              :         {
   11684            0 :           res = make_ssa_name (type);
   11685            0 :           gimple_call_set_lhs (stmt, res);
   11686              :         }
   11687            0 :       gimple_set_location (stmt, loc);
   11688            0 :       gimple_seq_add_stmt_without_update (&seq, stmt);
   11689              :     }
   11690            0 :   gimple_build_insert_seq (gsi, before, update, seq);
   11691            0 :   return res;
   11692              : }
   11693              : 
   11694              : /* Build CODE (OP0) with a result of type TYPE (or no result if TYPE is
   11695              :    void) with location LOC, simplifying it first if possible.  Returns the
   11696              :    built expression value (or NULL_TREE if TYPE is void) inserting any new
   11697              :    statements at GSI honoring BEFORE and UPDATE.  */
   11698              : 
   11699              : tree
   11700           21 : gimple_build (gimple_stmt_iterator *gsi,
   11701              :               bool before, gsi_iterator_update update,
   11702              :               location_t loc, code_helper code, tree type, tree op0)
   11703              : {
   11704           21 :   if (code.is_tree_code ())
   11705            0 :     return gimple_build (gsi, before, update, loc, tree_code (code), type, op0);
   11706           21 :   return gimple_build (gsi, before, update, loc, combined_fn (code), type, op0);
   11707              : }
   11708              : 
   11709              : /* Build CODE (OP0, OP1) with a result of type TYPE (or no result if TYPE is
   11710              :    void) with location LOC, simplifying it first if possible.  Returns the
   11711              :    built expression value (or NULL_TREE if TYPE is void) inserting any new
   11712              :    statements at GSI honoring BEFORE and UPDATE.  */
   11713              : 
   11714              : tree
   11715        23910 : gimple_build (gimple_stmt_iterator *gsi,
   11716              :               bool before, gsi_iterator_update update,
   11717              :               location_t loc, code_helper code, tree type, tree op0, tree op1)
   11718              : {
   11719        23910 :   if (code.is_tree_code ())
   11720        23772 :     return gimple_build (gsi, before, update,
   11721        23772 :                          loc, tree_code (code), type, op0, op1);
   11722          138 :   return gimple_build (gsi, before, update,
   11723          138 :                        loc, combined_fn (code), type, op0, op1);
   11724              : }
   11725              : 
   11726              : /* Build CODE (OP0, OP1, OP2) with a result of type TYPE (or no result if TYPE
   11727              :    is void) with location LOC, simplifying it first if possible.  Returns the
   11728              :    built expression value (or NULL_TREE if TYPE is void) inserting any new
   11729              :    statements at GSI honoring BEFORE and UPDATE.  */
   11730              : 
   11731              : tree
   11732            0 : gimple_build (gimple_stmt_iterator *gsi,
   11733              :               bool before, gsi_iterator_update update,
   11734              :               location_t loc, code_helper code,
   11735              :               tree type, tree op0, tree op1, tree op2)
   11736              : {
   11737            0 :   if (code.is_tree_code ())
   11738            0 :     return gimple_build (gsi, before, update,
   11739            0 :                          loc, tree_code (code), type, op0, op1, op2);
   11740            0 :   return gimple_build (gsi, before, update,
   11741            0 :                        loc, combined_fn (code), type, op0, op1, op2);
   11742              : }
   11743              : 
   11744              : /* Build the conversion (TYPE) OP with a result of type TYPE
   11745              :    with location LOC if such conversion is necessary in GIMPLE,
   11746              :    simplifying it first.
   11747              :    Returns the built expression inserting any new statements
   11748              :    at GSI honoring BEFORE and UPDATE.  */
   11749              : 
   11750              : tree
   11751      2092254 : gimple_convert (gimple_stmt_iterator *gsi,
   11752              :                 bool before, gsi_iterator_update update,
   11753              :                 location_t loc, tree type, tree op)
   11754              : {
   11755      2092254 :   if (useless_type_conversion_p (type, TREE_TYPE (op)))
   11756              :     return op;
   11757       190223 :   return gimple_build (gsi, before, update, loc, NOP_EXPR, type, op);
   11758              : }
   11759              : 
   11760              : /* Build the conversion (ptrofftype) OP with a result of a type
   11761              :    compatible with ptrofftype with location LOC if such conversion
   11762              :    is necessary in GIMPLE, simplifying it first.
   11763              :    Returns the built expression value inserting any new statements
   11764              :    at GSI honoring BEFORE and UPDATE.  */
   11765              : 
   11766              : tree
   11767          210 : gimple_convert_to_ptrofftype (gimple_stmt_iterator *gsi,
   11768              :                               bool before, gsi_iterator_update update,
   11769              :                               location_t loc, tree op)
   11770              : {
   11771          210 :   if (ptrofftype_p (TREE_TYPE (op)))
   11772              :     return op;
   11773            0 :   return gimple_convert (gsi, before, update, loc, sizetype, op);
   11774              : }
   11775              : 
   11776              : /* Build a vector of type TYPE in which each element has the value OP.
   11777              :    Return a gimple value for the result, inserting any new statements
   11778              :    at GSI honoring BEFORE and UPDATE.  */
   11779              : 
   11780              : tree
   11781       334928 : gimple_build_vector_from_val (gimple_stmt_iterator *gsi,
   11782              :                               bool before, gsi_iterator_update update,
   11783              :                               location_t loc, tree type, tree op)
   11784              : {
   11785       334928 :   if (!TYPE_VECTOR_SUBPARTS (type).is_constant ()
   11786              :       && !CONSTANT_CLASS_P (op))
   11787              :     return gimple_build (gsi, before, update,
   11788              :                          loc, VEC_DUPLICATE_EXPR, type, op);
   11789              : 
   11790       334928 :   tree res, vec = build_vector_from_val (type, op);
   11791       334928 :   if (is_gimple_val (vec))
   11792              :     return vec;
   11793        29407 :   if (gimple_in_ssa_p (cfun))
   11794        29407 :     res = make_ssa_name (type);
   11795              :   else
   11796            0 :     res = create_tmp_reg (type);
   11797        29407 :   gimple_seq seq = NULL;
   11798        29407 :   gimple *stmt = gimple_build_assign (res, vec);
   11799        29407 :   gimple_set_location (stmt, loc);
   11800        29407 :   gimple_seq_add_stmt_without_update (&seq, stmt);
   11801        29407 :   gimple_build_insert_seq (gsi, before, update, seq);
   11802        29407 :   return res;
   11803              : }
   11804              : 
   11805              : /* Build a vector from BUILDER, handling the case in which some elements
   11806              :    are non-constant.  Return a gimple value for the result, inserting
   11807              :    any new instructions to GSI honoring BEFORE and UPDATE.
   11808              : 
   11809              :    BUILDER must not have a stepped encoding on entry.  This is because
   11810              :    the function is not geared up to handle the arithmetic that would
   11811              :    be needed in the variable case, and any code building a vector that
   11812              :    is known to be constant should use BUILDER->build () directly.
   11813              : 
   11814              :    If at least one element is non-constant and the type associated with BUILDER
   11815              :    is a variable-length vector type then the encoding used by BUILDER must have
   11816              :    two elements per pattern; the second (repeating) element of each pattern
   11817              :    must be constant zero, as must the first if it falls outside the minimum
   11818              :    vector length.  */
   11819              : 
   11820              : tree
   11821       386402 : gimple_build_vector (gimple_stmt_iterator *gsi,
   11822              :                      bool before, gsi_iterator_update update,
   11823              :                      location_t loc, tree_vector_builder *builder)
   11824              : {
   11825       386402 :   gcc_assert (builder->nelts_per_pattern () <= 2);
   11826       386402 :   unsigned int encoded_nelts = builder->encoded_nelts ();
   11827      1365368 :   for (unsigned int i = 0; i < encoded_nelts; ++i)
   11828      1094299 :     if (!CONSTANT_CLASS_P ((*builder)[i]))
   11829              :       {
   11830       115333 :         gimple_seq seq = NULL;
   11831       115333 :         tree type = builder->type ();
   11832       115333 :         unsigned int min_nelts
   11833       115333 :           = constant_lower_bound (TYPE_VECTOR_SUBPARTS (type));
   11834              : 
   11835              :         /* All vector builders produce an infinitely repeating sequence; we only
   11836              :            care about truncation of that sequence if the vector type has a
   11837              :            variable length.  */
   11838       115333 :         unsigned HOST_WIDE_INT nelts;
   11839       115333 :         if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
   11840              :           {
   11841              :             /* Initial elements outside the minimum vector length must be
   11842              :                constant zero because that is the only implicit value that a
   11843              :                CONSTRUCTOR node can have.  */
   11844              :             for (i = min_nelts; i < builder->npatterns (); ++i)
   11845              :               gcc_assert (zerop ((*builder)[i]));
   11846              : 
   11847              :             /* The builder must also produce an infinitely repeating tail of
   11848              :                constant zeros.  */
   11849              :             gcc_assert (builder->nelts_per_pattern () == 2);
   11850              :             for (i = builder->npatterns (); i < encoded_nelts; ++i)
   11851              :               gcc_assert (zerop ((*builder)[i]));
   11852              : 
   11853              :             nelts = min_nelts;
   11854              :           }
   11855              : 
   11856       115333 :         vec<constructor_elt, va_gc> *v;
   11857       115333 :         vec_alloc (v, nelts);
   11858       483982 :         for (i = 0; i < nelts; ++i)
   11859       253316 :           CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, builder->elt (i));
   11860              : 
   11861       115333 :         tree res;
   11862       115333 :         if (gimple_in_ssa_p (cfun))
   11863       115333 :           res = make_ssa_name (type);
   11864              :         else
   11865            0 :           res = create_tmp_reg (type);
   11866       115333 :         gimple *stmt = gimple_build_assign (res, build_constructor (type, v));
   11867       115333 :         gimple_set_location (stmt, loc);
   11868       115333 :         gimple_seq_add_stmt_without_update (&seq, stmt);
   11869       115333 :         gimple_build_insert_seq (gsi, before, update, seq);
   11870       115333 :         return res;
   11871              :       }
   11872       271069 :   return builder->build ();
   11873              : }
   11874              : 
   11875              : /* Emit gimple statements into &stmts that take a value given in OLD_SIZE
   11876              :    and generate a value guaranteed to be rounded upwards to ALIGN.
   11877              : 
   11878              :    Return the tree node representing this size, it is of TREE_TYPE TYPE.  */
   11879              : 
   11880              : tree
   11881            0 : gimple_build_round_up (gimple_stmt_iterator *gsi,
   11882              :                        bool before, gsi_iterator_update update,
   11883              :                        location_t loc, tree type,
   11884              :                        tree old_size, unsigned HOST_WIDE_INT align)
   11885              : {
   11886            0 :   unsigned HOST_WIDE_INT tg_mask = align - 1;
   11887              :   /* tree new_size = (old_size + tg_mask) & ~tg_mask;  */
   11888            0 :   gcc_assert (INTEGRAL_TYPE_P (type));
   11889            0 :   tree tree_mask = build_int_cst (type, tg_mask);
   11890            0 :   tree oversize = gimple_build (gsi, before, update,
   11891              :                                 loc, PLUS_EXPR, type, old_size, tree_mask);
   11892              : 
   11893            0 :   tree mask = build_int_cst (type, -align);
   11894            0 :   return gimple_build (gsi, before, update,
   11895            0 :                        loc, BIT_AND_EXPR, type, oversize, mask);
   11896              : }
   11897              : 
   11898              : /* Return true if the result of assignment STMT is known to be non-negative.
   11899              :    DEPTH is the current nesting depth of the query.  */
   11900              : 
   11901              : static bool
   11902      6185538 : gimple_assign_nonnegative_p (gimple *stmt, int depth)
   11903              : {
   11904      6185538 :   enum tree_code code = gimple_assign_rhs_code (stmt);
   11905      6185538 :   tree type = TREE_TYPE (gimple_assign_lhs (stmt));
   11906      6185538 :   switch (get_gimple_rhs_class (code))
   11907              :     {
   11908       473738 :     case GIMPLE_UNARY_RHS:
   11909       473738 :       return tree_unary_nonnegative_p (gimple_assign_rhs_code (stmt),
   11910              :                                        type,
   11911              :                                        gimple_assign_rhs1 (stmt),
   11912       473738 :                                        depth);
   11913      3415624 :     case GIMPLE_BINARY_RHS:
   11914      3415624 :       return tree_binary_nonnegative_p (gimple_assign_rhs_code (stmt),
   11915              :                                         type,
   11916              :                                         gimple_assign_rhs1 (stmt),
   11917              :                                         gimple_assign_rhs2 (stmt),
   11918      3415624 :                                         depth);
   11919              :     case GIMPLE_TERNARY_RHS:
   11920              :       return false;
   11921      2282856 :     case GIMPLE_SINGLE_RHS:
   11922      2282856 :       return tree_single_nonnegative_p (gimple_assign_rhs1 (stmt),
   11923      2282856 :                                         depth);
   11924              :     case GIMPLE_INVALID_RHS:
   11925              :       break;
   11926              :     }
   11927            0 :   gcc_unreachable ();
   11928              : }
   11929              : 
   11930              : /* Return true if return value of call STMT is known to be non-negative.
   11931              :    DEPTH is the current nesting depth of the query.  */
   11932              : 
   11933              : static bool
   11934     13621716 : gimple_call_nonnegative_p (gimple *stmt, int depth)
   11935              : {
   11936     13621716 :   tree arg0
   11937     13621716 :     = gimple_call_num_args (stmt) > 0 ? gimple_call_arg (stmt, 0) : NULL_TREE;
   11938     13621716 :   tree arg1
   11939     13621716 :     = gimple_call_num_args (stmt) > 1 ? gimple_call_arg (stmt, 1) : NULL_TREE;
   11940     13621716 :   tree lhs = gimple_call_lhs (stmt);
   11941     13621716 :   return (lhs
   11942     13621716 :           && tree_call_nonnegative_p (TREE_TYPE (lhs),
   11943              :                                       gimple_call_combined_fn (stmt),
   11944     13621716 :                                       arg0, arg1, depth));
   11945              : }
   11946              : 
   11947              : /* Return true if return value of call STMT is known to be non-negative.
   11948              :    DEPTH is the current nesting depth of the query.  */
   11949              : 
   11950              : static bool
   11951       981467 : gimple_phi_nonnegative_p (gimple *stmt, int depth)
   11952              : {
   11953      1347550 :   for (unsigned i = 0; i < gimple_phi_num_args (stmt); ++i)
   11954              :     {
   11955      1342179 :       tree arg = gimple_phi_arg_def (stmt, i);
   11956      1342179 :       if (!tree_single_nonnegative_p (arg, depth + 1))
   11957              :         return false;
   11958              :     }
   11959              :   return true;
   11960              : }
   11961              : 
   11962              : /* Return true if STMT is known to compute a non-negative value.
   11963              :    DEPTH is the current nesting depth of the query.  */
   11964              : 
   11965              : bool
   11966     21486172 : gimple_stmt_nonnegative_p (gimple *stmt, int depth)
   11967              : {
   11968     21486172 :   tree type = gimple_range_type (stmt);
   11969     21486172 :   if (type && frange::supports_p (type))
   11970              :     {
   11971      1464420 :       frange r;
   11972      1464420 :       bool sign;
   11973      1464420 :       if (get_global_range_query ()->range_of_stmt (r, stmt)
   11974      1464420 :           && r.signbit_p (sign))
   11975        25906 :         return !sign;
   11976      1464420 :     }
   11977     21460266 :   switch (gimple_code (stmt))
   11978              :     {
   11979      6185538 :     case GIMPLE_ASSIGN:
   11980      6185538 :       return gimple_assign_nonnegative_p (stmt, depth);
   11981     13621716 :     case GIMPLE_CALL:
   11982     13621716 :       return gimple_call_nonnegative_p (stmt, depth);
   11983       981467 :     case GIMPLE_PHI:
   11984       981467 :       return gimple_phi_nonnegative_p (stmt, depth);
   11985              :     default:
   11986              :       return false;
   11987              :     }
   11988              : }
   11989              : 
   11990              : /* Return true if the floating-point value computed by assignment STMT
   11991              :    is known to have an integer value.  We also allow +Inf, -Inf and NaN
   11992              :    to be considered integer values. Return false for signaling NaN.
   11993              : 
   11994              :    DEPTH is the current nesting depth of the query.  */
   11995              : 
   11996              : static bool
   11997        60589 : gimple_assign_integer_valued_real_p (gimple *stmt, int depth)
   11998              : {
   11999        60589 :   enum tree_code code = gimple_assign_rhs_code (stmt);
   12000        60589 :   switch (get_gimple_rhs_class (code))
   12001              :     {
   12002        15227 :     case GIMPLE_UNARY_RHS:
   12003        15227 :       return integer_valued_real_unary_p (gimple_assign_rhs_code (stmt),
   12004        15227 :                                           gimple_assign_rhs1 (stmt), depth);
   12005        13784 :     case GIMPLE_BINARY_RHS:
   12006        13784 :       return integer_valued_real_binary_p (gimple_assign_rhs_code (stmt),
   12007              :                                            gimple_assign_rhs1 (stmt),
   12008        13784 :                                            gimple_assign_rhs2 (stmt), depth);
   12009              :     case GIMPLE_TERNARY_RHS:
   12010              :       return false;
   12011        30394 :     case GIMPLE_SINGLE_RHS:
   12012        30394 :       return integer_valued_real_single_p (gimple_assign_rhs1 (stmt), depth);
   12013              :     case GIMPLE_INVALID_RHS:
   12014              :       break;
   12015              :     }
   12016            0 :   gcc_unreachable ();
   12017              : }
   12018              : 
   12019              : /* Return true if the floating-point value computed by call STMT is known
   12020              :    to have an integer value.  We also allow +Inf, -Inf and NaN to be
   12021              :    considered integer values. Return false for signaling NaN.
   12022              : 
   12023              :    DEPTH is the current nesting depth of the query.  */
   12024              : 
   12025              : static bool
   12026         1125 : gimple_call_integer_valued_real_p (gimple *stmt, int depth)
   12027              : {
   12028         1125 :   tree arg0 = (gimple_call_num_args (stmt) > 0
   12029         1125 :                ? gimple_call_arg (stmt, 0)
   12030              :                : NULL_TREE);
   12031         1125 :   tree arg1 = (gimple_call_num_args (stmt) > 1
   12032         1125 :                ? gimple_call_arg (stmt, 1)
   12033              :                : NULL_TREE);
   12034         1125 :   return integer_valued_real_call_p (gimple_call_combined_fn (stmt),
   12035         1125 :                                      arg0, arg1, depth);
   12036              : }
   12037              : 
   12038              : /* Return true if the floating-point result of phi STMT is known to have
   12039              :    an integer value.  We also allow +Inf, -Inf and NaN to be considered
   12040              :    integer values. Return false for signaling NaN.
   12041              : 
   12042              :    DEPTH is the current nesting depth of the query.  */
   12043              : 
   12044              : static bool
   12045         1569 : gimple_phi_integer_valued_real_p (gimple *stmt, int depth)
   12046              : {
   12047         1736 :   for (unsigned i = 0; i < gimple_phi_num_args (stmt); ++i)
   12048              :     {
   12049         1727 :       tree arg = gimple_phi_arg_def (stmt, i);
   12050         1727 :       if (!integer_valued_real_single_p (arg, depth + 1))
   12051              :         return false;
   12052              :     }
   12053              :   return true;
   12054              : }
   12055              : 
   12056              : /* Return true if the floating-point value computed by STMT is known
   12057              :    to have an integer value.  We also allow +Inf, -Inf and NaN to be
   12058              :    considered integer values. Return false for signaling NaN.
   12059              : 
   12060              :    DEPTH is the current nesting depth of the query.  */
   12061              : 
   12062              : bool
   12063        90675 : gimple_stmt_integer_valued_real_p (gimple *stmt, int depth)
   12064              : {
   12065        90675 :   switch (gimple_code (stmt))
   12066              :     {
   12067        60589 :     case GIMPLE_ASSIGN:
   12068        60589 :       return gimple_assign_integer_valued_real_p (stmt, depth);
   12069         1125 :     case GIMPLE_CALL:
   12070         1125 :       return gimple_call_integer_valued_real_p (stmt, depth);
   12071         1569 :     case GIMPLE_PHI:
   12072         1569 :       return gimple_phi_integer_valued_real_p (stmt, depth);
   12073              :     default:
   12074              :       return false;
   12075              :     }
   12076              : }
        

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