LCOV - code coverage report
Current view: top level - gcc/c - c-fold.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 96.6 % 439 424
Test Date: 2026-10-03 16:17:38 Functions: 100.0 % 6 6
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Support for fully folding sub-trees of an expression for C compiler.
       2              :    Copyright (C) 1992-2026 Free Software Foundation, Inc.
       3              : 
       4              : This file is part of GCC.
       5              : 
       6              : GCC is free software; you can redistribute it and/or modify it under
       7              : the terms of the GNU General Public License as published by the Free
       8              : Software Foundation; either version 3, or (at your option) any later
       9              : version.
      10              : 
      11              : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      12              : WARRANTY; without even the implied warranty of MERCHANTABILITY or
      13              : FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      14              : for more details.
      15              : 
      16              : You should have received a copy of the GNU General Public License
      17              : along with GCC; see the file COPYING3.  If not see
      18              : <http://www.gnu.org/licenses/>.  */
      19              : 
      20              : #include "config.h"
      21              : #include "system.h"
      22              : #include "coretypes.h"
      23              : #include "target.h"
      24              : #include "function.h"
      25              : #include "bitmap.h"
      26              : #include "c-tree.h"
      27              : #include "intl.h"
      28              : #include "gimplify.h"
      29              : 
      30              : static tree c_fully_fold_internal (tree expr, bool, bool *, bool *, bool,
      31              :                                    bool);
      32              : 
      33              : /* If DISABLE is true, stop issuing warnings.  This is used when
      34              :    parsing code that we know will not be executed.  This function may
      35              :    be called multiple times, and works as a stack.  */
      36              : 
      37              : static void
      38       973312 : c_disable_warnings (bool disable)
      39              : {
      40            0 :   if (disable)
      41              :     {
      42        70672 :       ++c_inhibit_evaluation_warnings;
      43              :     }
      44            0 : }
      45              : 
      46              : /* If ENABLE is true, re-enable issuing warnings.  */
      47              : 
      48              : static void
      49       973312 : c_enable_warnings (bool enable)
      50              : {
      51            0 :   if (enable)
      52              :     {
      53        70672 :       --c_inhibit_evaluation_warnings;
      54              :     }
      55            0 : }
      56              : 
      57              : /* Try to fold ARRAY_REF ary[index] if possible and not handled by
      58              :    normal fold, return NULL_TREE otherwise.  */
      59              : 
      60              : static tree
      61          812 : c_fold_array_ref (tree type, tree ary, tree index)
      62              : {
      63          812 :   if (TREE_CODE (ary) != STRING_CST
      64          792 :       || TREE_CODE (index) != INTEGER_CST
      65          792 :       || TREE_OVERFLOW (index)
      66          791 :       || TREE_CODE (TREE_TYPE (ary)) != ARRAY_TYPE
      67         1603 :       || !tree_fits_uhwi_p (index))
      68              :     return NULL_TREE;
      69              : 
      70          790 :   tree elem_type = TREE_TYPE (TREE_TYPE (ary));
      71          790 :   unsigned elem_nchars = (TYPE_PRECISION (elem_type)
      72          790 :                           / TYPE_PRECISION (char_type_node));
      73          790 :   unsigned len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
      74          790 :   tree nelts_minus_one = array_type_nelts_minus_one (TREE_TYPE (ary));
      75          790 :   bool dummy1 = true, dummy2 = true;
      76          790 :   nelts_minus_one = c_fully_fold_internal (nelts_minus_one, true, &dummy1,
      77              :                                            &dummy2, false, false);
      78          790 :   unsigned HOST_WIDE_INT i = tree_to_uhwi (index);
      79          790 :   if (!tree_int_cst_le (index, nelts_minus_one)
      80          789 :       || i >= len
      81         1579 :       || i + elem_nchars > len)
      82              :     return NULL_TREE;
      83              : 
      84          789 :   if (elem_nchars == 1)
      85          789 :     return build_int_cst (type, TREE_STRING_POINTER (ary)[i]);
      86              : 
      87            0 :   const unsigned char *ptr
      88            0 :     = ((const unsigned char *)TREE_STRING_POINTER (ary) + i * elem_nchars);
      89            0 :   return native_interpret_expr (type, ptr, elem_nchars);
      90              : }
      91              : 
      92              : /* Fully fold EXPR, an expression that was not folded (beyond integer
      93              :    constant expressions and null pointer constants) when being built
      94              :    up.  If IN_INIT, this is in a static initializer and certain
      95              :    changes are made to the folding done.  Clear *MAYBE_CONST if
      96              :    MAYBE_CONST is not NULL and EXPR is definitely not a constant
      97              :    expression because it contains an evaluated operator (in C99) or an
      98              :    operator outside of sizeof returning an integer constant (in C90)
      99              :    not permitted in constant expressions, or because it contains an
     100              :    evaluated arithmetic overflow.  (*MAYBE_CONST should typically be
     101              :    set to true by callers before calling this function.)  Return the
     102              :    folded expression.  Function arguments have already been folded
     103              :    before calling this function, as have the contents of SAVE_EXPR,
     104              :    TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
     105              :    C_MAYBE_CONST_EXPR.  LVAL is true if it should be treated as an
     106              :    lvalue.  */
     107              : 
     108              : tree
     109    276910254 : c_fully_fold (tree expr, bool in_init, bool *maybe_const, bool lval)
     110              : {
     111    276910254 :   tree ret;
     112    276910254 :   tree eptype = NULL_TREE;
     113    276910254 :   bool dummy = true;
     114    276910254 :   bool maybe_const_itself = true;
     115    276910254 :   location_t loc = EXPR_LOCATION (expr);
     116              : 
     117    276910254 :   if (!maybe_const)
     118    241194733 :     maybe_const = &dummy;
     119    276910254 :   if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
     120              :     {
     121          751 :       eptype = TREE_TYPE (expr);
     122          751 :       expr = TREE_OPERAND (expr, 0);
     123              :     }
     124    276910254 :   ret = c_fully_fold_internal (expr, in_init, maybe_const,
     125              :                                &maybe_const_itself, false, lval);
     126    276910254 :   if (eptype)
     127          751 :     ret = fold_convert_loc (loc, eptype, ret);
     128    276910254 :   *maybe_const &= maybe_const_itself;
     129    276910254 :   return ret;
     130              : }
     131              : 
     132              : /* Internal helper for c_fully_fold.  EXPR and IN_INIT are as for
     133              :    c_fully_fold.  *MAYBE_CONST_OPERANDS is cleared because of operands
     134              :    not permitted, while *MAYBE_CONST_ITSELF is cleared because of
     135              :    arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
     136              :    both evaluated and unevaluated subexpressions while
     137              :    *MAYBE_CONST_ITSELF is carried from only evaluated
     138              :    subexpressions).  FOR_INT_CONST indicates if EXPR is an expression
     139              :    with integer constant operands, and if any of the operands doesn't
     140              :    get folded to an integer constant, don't fold the expression itself.
     141              :    LVAL indicates folding of lvalue, where we can't replace it with
     142              :    an rvalue.  */
     143              : 
     144              : static tree
     145    498493724 : c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
     146              :                        bool *maybe_const_itself, bool for_int_const, bool lval)
     147              : {
     148    498493724 :   tree ret = expr;
     149    498493724 :   enum tree_code code = TREE_CODE (expr);
     150    498493724 :   enum tree_code_class kind = TREE_CODE_CLASS (code);
     151    498493724 :   location_t loc = EXPR_LOCATION (expr);
     152    498493724 :   tree op0, op1, op2, op3;
     153    498493724 :   tree orig_op0, orig_op1, orig_op2;
     154    498493724 :   bool op0_const = true, op1_const = true, op2_const = true;
     155    498493724 :   bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
     156    498493724 :   bool nowarning = warning_suppressed_p (expr, OPT_Woverflow);
     157    498493724 :   bool unused_p;
     158    498493724 :   bool op0_lval = false;
     159    498493724 :   source_range old_range;
     160              : 
     161              :   /* Constants, declarations, statements, errors, and anything else not
     162              :      counted as an expression cannot usefully be folded further at this
     163              :      point.  */
     164    498493724 :   if (!IS_EXPR_CODE_CLASS (kind) || kind == tcc_statement)
     165              :     {
     166              :       /* Except for variables which we can optimize to its initializer.  */
     167    234168090 :       if (VAR_P (expr) && !lval && (optimize || in_init))
     168              :         {
     169     14428451 :           if (in_init)
     170          466 :             ret = decl_constant_value_1 (expr, true);
     171              :           else
     172              :             {
     173     14428243 :               ret = decl_constant_value (expr);
     174     14428243 :               if (ret != expr
     175     14428243 :                   && (TYPE_MODE (TREE_TYPE (ret)) == BLKmode
     176        15223 :                       || TREE_CODE (TREE_TYPE (ret)) == ARRAY_TYPE))
     177              :                 return expr;
     178              :             }
     179              :           /* Avoid unwanted tree sharing between the initializer and current
     180              :              function's body where the tree can be modified e.g. by the
     181              :              gimplifier.  */
     182     14426180 :           if (ret != expr && TREE_STATIC (expr))
     183         5867 :             ret = unshare_expr (ret);
     184              :           return ret;
     185              :         }
     186              :       return expr;
     187              :     }
     188              : 
     189    264325634 :   if (IS_EXPR_CODE_CLASS (kind))
     190    264325634 :     old_range = EXPR_LOCATION_RANGE (expr);
     191              : 
     192              :   /* Operands of variable-length expressions (function calls) have
     193              :      already been folded, as have __builtin_* function calls, and such
     194              :      expressions cannot occur in constant expressions.  */
     195    264325634 :   if (kind == tcc_vl_exp)
     196              :     {
     197     51880748 :       *maybe_const_operands = false;
     198     51880748 :       ret = fold (expr);
     199     51880748 :       goto out;
     200              :     }
     201              : 
     202    212444886 :   if (code == C_MAYBE_CONST_EXPR)
     203              :     {
     204      4570310 :       tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
     205      4570310 :       tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
     206      4570310 :       if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
     207      2180818 :         *maybe_const_operands = false;
     208      4570310 :       if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
     209              :         {
     210         1715 :           *maybe_const_itself = false;
     211         1715 :           inner = c_fully_fold_internal (inner, in_init, maybe_const_operands,
     212              :                                          maybe_const_itself, true, lval);
     213              :         }
     214      4570310 :       if (pre && !in_init)
     215          671 :         ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
     216              :       else
     217              :         ret = inner;
     218      4570310 :       goto out;
     219              :     }
     220              : 
     221              :   /* Assignment, increment, decrement, function call and comma
     222              :      operators, and statement expressions, cannot occur in constant
     223              :      expressions if evaluated / outside of sizeof.  (Function calls
     224              :      were handled above, though VA_ARG_EXPR is treated like a function
     225              :      call here, and statement expressions are handled through
     226              :      C_MAYBE_CONST_EXPR to avoid folding inside them.)  */
     227    207874576 :   switch (code)
     228              :     {
     229      4186810 :     case MODIFY_EXPR:
     230      4186810 :     case PREDECREMENT_EXPR:
     231      4186810 :     case PREINCREMENT_EXPR:
     232      4186810 :     case POSTDECREMENT_EXPR:
     233      4186810 :     case POSTINCREMENT_EXPR:
     234      4186810 :     case COMPOUND_EXPR:
     235      4186810 :       *maybe_const_operands = false;
     236      4186810 :       break;
     237              : 
     238       139703 :     case VA_ARG_EXPR:
     239       139703 :     case TARGET_EXPR:
     240       139703 :     case BIND_EXPR:
     241       139703 :     case OBJ_TYPE_REF:
     242       139703 :       *maybe_const_operands = false;
     243       139703 :       ret = fold (expr);
     244       139703 :       goto out;
     245              : 
     246              :     default:
     247              :       break;
     248              :     }
     249              : 
     250              :   /* Fold individual tree codes as appropriate.  */
     251    207734873 :   switch (code)
     252              :     {
     253       924106 :     case COMPOUND_LITERAL_EXPR:
     254              :       /* Any non-constancy will have been marked in a containing
     255              :          C_MAYBE_CONST_EXPR; there is no more folding to do here.  */
     256       924106 :       goto out;
     257              : 
     258      2825869 :     case COMPONENT_REF:
     259      2825869 :       orig_op0 = op0 = TREE_OPERAND (expr, 0);
     260      2825869 :       op1 = TREE_OPERAND (expr, 1);
     261      2825869 :       op2 = TREE_OPERAND (expr, 2);
     262      2825869 :       op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
     263              :                                    maybe_const_itself, for_int_const, lval);
     264      5651738 :       STRIP_TYPE_NOPS (op0);
     265      2825869 :       if (op0 != orig_op0)
     266        10391 :         ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
     267      2825869 :       if (ret != expr)
     268              :         {
     269        10391 :           TREE_READONLY (ret) = TREE_READONLY (expr);
     270        10391 :           TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
     271              :         }
     272      2825869 :       if (!lval)
     273      2053185 :         ret = fold (ret);
     274      2825869 :       goto out;
     275              : 
     276      2937008 :     case ARRAY_REF:
     277      2937008 :       orig_op0 = op0 = TREE_OPERAND (expr, 0);
     278      2937008 :       orig_op1 = op1 = TREE_OPERAND (expr, 1);
     279      2937008 :       op2 = TREE_OPERAND (expr, 2);
     280      2937008 :       op3 = TREE_OPERAND (expr, 3);
     281      2937008 :       op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
     282              :                                    maybe_const_itself, for_int_const, lval);
     283      5874016 :       STRIP_TYPE_NOPS (op0);
     284      2937008 :       op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
     285              :                                    maybe_const_itself, for_int_const, false);
     286      5874016 :       STRIP_TYPE_NOPS (op1);
     287              :       /* Fold "foo"[2] in initializers.  */
     288      2937008 :       if (!lval && in_init)
     289              :         {
     290          812 :           ret = c_fold_array_ref (TREE_TYPE (expr), op0, op1);
     291          812 :           if (ret)
     292          789 :             goto out;
     293              :           ret = expr;
     294              :         }
     295      2936219 :       if (op0 != orig_op0 || op1 != orig_op1)
     296       134341 :         ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
     297      2936219 :       if (ret != expr)
     298              :         {
     299       134341 :           TREE_READONLY (ret) = TREE_READONLY (expr);
     300       134341 :           TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
     301       134341 :           TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
     302              :         }
     303      2936219 :       if (!lval)
     304      2358423 :         ret = fold (ret);
     305      2936219 :       goto out;
     306              : 
     307      3924738 :     case MODIFY_EXPR:
     308      3924738 :     case PREDECREMENT_EXPR:
     309      3924738 :     case PREINCREMENT_EXPR:
     310      3924738 :     case POSTDECREMENT_EXPR:
     311      3924738 :     case POSTINCREMENT_EXPR:
     312      3924738 :       op0_lval = true;
     313              :       /* FALLTHRU */
     314     13719793 :     case COMPOUND_EXPR:
     315     13719793 :     case PLUS_EXPR:
     316     13719793 :     case MINUS_EXPR:
     317     13719793 :     case MULT_EXPR:
     318     13719793 :     case POINTER_PLUS_EXPR:
     319     13719793 :     case POINTER_DIFF_EXPR:
     320     13719793 :     case TRUNC_DIV_EXPR:
     321     13719793 :     case CEIL_DIV_EXPR:
     322     13719793 :     case FLOOR_DIV_EXPR:
     323     13719793 :     case TRUNC_MOD_EXPR:
     324     13719793 :     case RDIV_EXPR:
     325     13719793 :     case EXACT_DIV_EXPR:
     326     13719793 :     case LSHIFT_EXPR:
     327     13719793 :     case RSHIFT_EXPR:
     328     13719793 :     case LROTATE_EXPR:
     329     13719793 :     case RROTATE_EXPR:
     330     13719793 :     case BIT_IOR_EXPR:
     331     13719793 :     case BIT_XOR_EXPR:
     332     13719793 :     case BIT_AND_EXPR:
     333     13719793 :     case LT_EXPR:
     334     13719793 :     case LE_EXPR:
     335     13719793 :     case GT_EXPR:
     336     13719793 :     case GE_EXPR:
     337     13719793 :     case EQ_EXPR:
     338     13719793 :     case NE_EXPR:
     339     13719793 :     case COMPLEX_EXPR:
     340     13719793 :     case TRUTH_AND_EXPR:
     341     13719793 :     case TRUTH_OR_EXPR:
     342     13719793 :     case TRUTH_XOR_EXPR:
     343     13719793 :     case UNORDERED_EXPR:
     344     13719793 :     case ORDERED_EXPR:
     345     13719793 :     case UNLT_EXPR:
     346     13719793 :     case UNLE_EXPR:
     347     13719793 :     case UNGT_EXPR:
     348     13719793 :     case UNGE_EXPR:
     349     13719793 :     case UNEQ_EXPR:
     350     13719793 :     case MEM_REF:
     351              :       /* Binary operations evaluating both arguments (increment and
     352              :          decrement are binary internally in GCC).  */
     353     13719793 :       orig_op0 = op0 = TREE_OPERAND (expr, 0);
     354     13719793 :       orig_op1 = op1 = TREE_OPERAND (expr, 1);
     355     13719793 :       op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
     356              :                                    maybe_const_itself, for_int_const,
     357              :                                    op0_lval);
     358     27439586 :       STRIP_TYPE_NOPS (op0);
     359              :       /* The RHS of a MODIFY_EXPR was fully folded when building that
     360              :          expression for the sake of conversion warnings.  */
     361     13719793 :       if (code != MODIFY_EXPR)
     362     10745944 :         op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
     363              :                                      maybe_const_itself, for_int_const, false);
     364     13719851 :       STRIP_TYPE_NOPS (op1);
     365              : 
     366     13719793 :       if (for_int_const && (TREE_CODE (op0) != INTEGER_CST
     367         1282 :                             || TREE_CODE (op1) != INTEGER_CST))
     368          273 :         goto out;
     369              : 
     370     13719520 :       if (TREE_CODE_CLASS (code) == tcc_comparison
     371      2488956 :           && TREE_CODE (TREE_TYPE (op0)) == NULLPTR_TYPE
     372     13719542 :           && TREE_CODE (TREE_TYPE (op1)) == NULLPTR_TYPE)
     373              :         {
     374           22 :           switch (code)
     375              :             {
     376            8 :             case EQ_EXPR:
     377            8 :               ret = constant_boolean_node (true, TREE_TYPE (expr));
     378            8 :               break;
     379           14 :             case NE_EXPR:
     380           14 :               ret = constant_boolean_node (false, TREE_TYPE (expr));
     381           14 :               break;
     382            0 :             default:
     383            0 :               gcc_unreachable ();
     384              :             }
     385           22 :           ret = omit_two_operands_loc (loc, TREE_TYPE (expr), ret,
     386              :                                        op0, op1);
     387              :         }
     388     13719498 :       else if (op0 != orig_op0 || op1 != orig_op1 || in_init)
     389      2573672 :         ret = in_init
     390      2575377 :           ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
     391      2571158 :           : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
     392              :       else
     393     11144121 :         ret = fold (expr);
     394       268291 :       if (TREE_OVERFLOW_P (ret)
     395           22 :           && !TREE_OVERFLOW_P (op0)
     396           21 :           && !(BINARY_CLASS_P (op0) && TREE_OVERFLOW_P (TREE_OPERAND (op0, 1)))
     397     13719540 :           && !TREE_OVERFLOW_P (op1))
     398           19 :         overflow_warning (EXPR_LOC_OR_LOC (expr, input_location), ret, expr);
     399     13719520 :       if (code == LSHIFT_EXPR
     400       453299 :           && TREE_CODE (orig_op0) != INTEGER_CST
     401       368316 :           && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
     402          887 :               || TREE_CODE (TREE_TYPE (orig_op0)) == BITINT_TYPE)
     403       367773 :           && TREE_CODE (op0) == INTEGER_CST
     404          175 :           && c_inhibit_evaluation_warnings == 0
     405           76 :           && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (orig_op0))
     406     13719552 :           && tree_int_cst_sgn (op0) < 0)
     407            6 :         warning_at (loc, OPT_Wshift_negative_value,
     408              :                     "left shift of negative value");
     409     13719520 :       if ((code == LSHIFT_EXPR
     410              :            || code == RSHIFT_EXPR
     411              :            || code == LROTATE_EXPR
     412     13719520 :            || code == RROTATE_EXPR)
     413       643242 :           && TREE_CODE (orig_op1) != INTEGER_CST
     414       145781 :           && TREE_CODE (op1) == INTEGER_CST
     415          521 :           && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
     416     13720039 :           && c_inhibit_evaluation_warnings == 0)
     417              :         {
     418          329 :           if (tree_int_cst_sgn (op1) < 0)
     419           29 :             warning_at (loc, OPT_Wshift_count_negative,
     420              :                         (code == LSHIFT_EXPR
     421              :                          ? G_("left shift count is negative")
     422              :                          : G_("right shift count is negative")));
     423          312 :           else if ((TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
     424          106 :                     || TREE_CODE (TREE_TYPE (orig_op0)) == BITINT_TYPE
     425           12 :                     || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
     426          300 :                    && compare_tree_int (op1,
     427          300 :                                         TYPE_PRECISION (TREE_TYPE (orig_op0)))
     428              :                       >= 0
     429          327 :                    && !warning_suppressed_p (expr, OPT_Wshift_count_overflow))
     430            6 :             warning_at (loc, OPT_Wshift_count_overflow,
     431              :                         (code == LSHIFT_EXPR
     432              :                          ? G_("left shift count >= width of type")
     433              :                          : G_("right shift count >= width of type")));
     434          308 :           else if (TREE_CODE (TREE_TYPE (orig_op0)) == VECTOR_TYPE
     435          320 :                    && compare_tree_int (op1,
     436           12 :                                         TYPE_PRECISION (TREE_TYPE (TREE_TYPE (orig_op0))))
     437              :                       >= 0)
     438            9 :             warning_at (loc, OPT_Wshift_count_overflow,
     439              :                         code == LSHIFT_EXPR
     440              :                         ? G_("left shift count >= width of vector element")
     441              :                         : G_("right shift count >= width of vector element"));
     442              :         }
     443     13719520 :       if (code == LSHIFT_EXPR
     444              :           /* If either OP0 has been folded to INTEGER_CST...  */
     445       453299 :           && ((TREE_CODE (orig_op0) != INTEGER_CST
     446       368316 :                && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
     447          887 :                    || TREE_CODE (TREE_TYPE (orig_op0)) == BITINT_TYPE)
     448       367773 :                && TREE_CODE (op0) == INTEGER_CST)
     449              :               /* ...or if OP1 has been folded to INTEGER_CST...  */
     450       453124 :               || (TREE_CODE (orig_op1) != INTEGER_CST
     451       114279 :                   && (TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
     452          340 :                       || TREE_CODE (TREE_TYPE (orig_op1)) == BITINT_TYPE)
     453       114017 :                   && TREE_CODE (op1) == INTEGER_CST))
     454     13719911 :           && c_inhibit_evaluation_warnings == 0)
     455              :         /* ...then maybe we can detect an overflow.  */
     456          211 :         maybe_warn_shift_overflow (loc, op0, op1);
     457     13719520 :       if ((code == TRUNC_DIV_EXPR
     458              :            || code == CEIL_DIV_EXPR
     459     13719520 :            || code == FLOOR_DIV_EXPR
     460     13719520 :            || code == EXACT_DIV_EXPR
     461              :            || code == TRUNC_MOD_EXPR)
     462        89710 :           && TREE_CODE (orig_op1) != INTEGER_CST
     463        12071 :           && TREE_CODE (op1) == INTEGER_CST
     464           97 :           && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
     465            0 :               || TREE_CODE (TREE_TYPE (orig_op0)) == BITINT_TYPE
     466            0 :               || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
     467           97 :           && (TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
     468            0 :               || TREE_CODE (TREE_TYPE (orig_op1)) == BITINT_TYPE))
     469           97 :         warn_for_div_by_zero (loc, op1);
     470     13719520 :       if (code == MEM_REF
     471     13719520 :           && ret != expr
     472        12327 :           && TREE_CODE (ret) == MEM_REF)
     473              :         {
     474        12327 :           TREE_READONLY (ret) = TREE_READONLY (expr);
     475        12327 :           TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
     476        12327 :           TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
     477              :         }
     478     13719520 :       goto out;
     479              : 
     480     54422907 :     case ADDR_EXPR:
     481     54422907 :       op0_lval = true;
     482     54422907 :       goto unary;
     483     97317240 :     case REALPART_EXPR:
     484     97317240 :     case IMAGPART_EXPR:
     485     97317240 :     case VIEW_CONVERT_EXPR:
     486     97317240 :       op0_lval = lval;
     487              :       /* FALLTHRU */
     488    186356442 :     case INDIRECT_REF:
     489    186356442 :     case FIX_TRUNC_EXPR:
     490    186356442 :     case FLOAT_EXPR:
     491    186356442 :     CASE_CONVERT:
     492    186356442 :     case ADDR_SPACE_CONVERT_EXPR:
     493    186356442 :     case NON_LVALUE_EXPR:
     494    186356442 :     case NEGATE_EXPR:
     495    186356442 :     case BIT_NOT_EXPR:
     496    186356442 :     case TRUTH_NOT_EXPR:
     497    186356442 :     case CONJ_EXPR:
     498    186356442 :     case PAREN_EXPR:
     499    186356442 :     unary:
     500              :       /* Unary operations.  */
     501    186356442 :       orig_op0 = op0 = TREE_OPERAND (expr, 0);
     502    186356442 :       op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
     503              :                                    maybe_const_itself, for_int_const,
     504              :                                    op0_lval);
     505    372712906 :       STRIP_TYPE_NOPS (op0);
     506              : 
     507    186356442 :       if (for_int_const && TREE_CODE (op0) != INTEGER_CST)
     508           14 :         goto out;
     509              : 
     510              :       /* ??? Cope with user tricks that amount to offsetof.  The middle-end is
     511              :          not prepared to deal with them if they occur in initializers.  */
     512    186356428 :       if (op0 != orig_op0
     513    186356428 :           && code == ADDR_EXPR
     514          509 :           && (op1 = get_base_address (op0)) != NULL_TREE
     515          509 :           && INDIRECT_REF_P (op1)
     516    186356591 :           && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
     517          102 :         ret = fold_offsetof (op0, TREE_TYPE (expr));
     518    186356326 :       else if (op0 != orig_op0 || in_init)
     519      4183095 :         ret = in_init
     520      4183095 :           ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
     521      1212708 :           : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
     522              :       else
     523    182173231 :         ret = fold (expr);
     524    186356428 :       if (code == INDIRECT_REF
     525    186356428 :           && ret != expr
     526        39155 :           && INDIRECT_REF_P (ret))
     527              :         {
     528        39057 :           TREE_READONLY (ret) = TREE_READONLY (expr);
     529        39057 :           TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
     530        39057 :           TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
     531              :         }
     532    186356428 :       switch (code)
     533              :         {
     534              :         case FIX_TRUNC_EXPR:
     535              :         case FLOAT_EXPR:
     536              :         CASE_CONVERT:
     537              :           /* Don't warn about explicit conversions.  We will already
     538              :              have warned about suspect implicit conversions.  */
     539              :           break;
     540              : 
     541    155862858 :         default:
     542    155862858 :           if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
     543            0 :             overflow_warning (EXPR_LOCATION (expr), ret, op0);
     544              :           break;
     545              :         }
     546    186356428 :       goto out;
     547              : 
     548       445852 :     case TRUTH_ANDIF_EXPR:
     549       445852 :     case TRUTH_ORIF_EXPR:
     550              :       /* Binary operations not necessarily evaluating both
     551              :          arguments.  */
     552       445852 :       orig_op0 = op0 = TREE_OPERAND (expr, 0);
     553       445852 :       orig_op1 = op1 = TREE_OPERAND (expr, 1);
     554       445852 :       op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self,
     555              :                                    for_int_const, false);
     556       891704 :       STRIP_TYPE_NOPS (op0);
     557              : 
     558       891704 :       unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
     559       445852 :                           ? truthvalue_false_node
     560              :                           : truthvalue_true_node));
     561       445852 :       c_disable_warnings (unused_p);
     562       445852 :       op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self,
     563              :                                    for_int_const, false);
     564       891704 :       STRIP_TYPE_NOPS (op1);
     565       445852 :       c_enable_warnings (unused_p);
     566              : 
     567       445852 :       if (for_int_const
     568           50 :           && (TREE_CODE (op0) != INTEGER_CST
     569              :               /* Require OP1 be an INTEGER_CST only if it's evaluated.  */
     570           36 :               || (!unused_p && TREE_CODE (op1) != INTEGER_CST)))
     571           40 :         goto out;
     572              : 
     573       445812 :       if (op0 != orig_op0 || op1 != orig_op1 || in_init)
     574       436210 :         ret = in_init
     575       436210 :           ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
     576       436195 :           : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
     577              :       else
     578         9602 :         ret = fold (expr);
     579       445812 :       *maybe_const_operands &= op0_const;
     580       445812 :       *maybe_const_itself &= op0_const_self;
     581       788826 :       if (!(flag_isoc99
     582       442288 :             && op0_const
     583       343014 :             && op0_const_self
     584              :             && (code == TRUTH_ANDIF_EXPR
     585       127399 :                 ? op0 == truthvalue_false_node
     586       215615 :                 : op0 == truthvalue_true_node)))
     587       434313 :         *maybe_const_operands &= op1_const;
     588       791599 :       if (!(op0_const
     589       345787 :             && op0_const_self
     590              :             && (code == TRUTH_ANDIF_EXPR
     591       127687 :                 ? op0 == truthvalue_false_node
     592       218100 :                 : op0 == truthvalue_true_node)))
     593       434297 :         *maybe_const_itself &= op1_const_self;
     594       445812 :       goto out;
     595              : 
     596       263730 :     case COND_EXPR:
     597       263730 :       orig_op0 = op0 = TREE_OPERAND (expr, 0);
     598       263730 :       orig_op1 = op1 = TREE_OPERAND (expr, 1);
     599       263730 :       orig_op2 = op2 = TREE_OPERAND (expr, 2);
     600       263730 :       op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self,
     601              :                                    for_int_const, false);
     602              : 
     603       527460 :       STRIP_TYPE_NOPS (op0);
     604       263730 :       c_disable_warnings (op0 == truthvalue_false_node);
     605       263730 :       op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self,
     606              :                                    for_int_const, false);
     607       527460 :       STRIP_TYPE_NOPS (op1);
     608       263730 :       c_enable_warnings (op0 == truthvalue_false_node);
     609              : 
     610       263730 :       c_disable_warnings (op0 == truthvalue_true_node);
     611       263730 :       op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self,
     612              :                                    for_int_const, false);
     613       527466 :       STRIP_TYPE_NOPS (op2);
     614       263730 :       c_enable_warnings (op0 == truthvalue_true_node);
     615              : 
     616       263771 :       if (for_int_const
     617           76 :           && (TREE_CODE (op0) != INTEGER_CST
     618              :               /* Only the evaluated operand must be an INTEGER_CST.  */
     619           14 :               || (op0 == truthvalue_true_node
     620           14 :                   ? TREE_CODE (op1) != INTEGER_CST
     621           27 :                   : TREE_CODE (op2) != INTEGER_CST)))
     622           62 :         goto out;
     623              : 
     624       263668 :       if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
     625       207713 :         ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
     626              :       else
     627        55955 :         ret = fold (expr);
     628       263668 :       *maybe_const_operands &= op0_const;
     629       263668 :       *maybe_const_itself &= op0_const_self;
     630       263668 :       if (!(flag_isoc99
     631       262338 :             && op0_const
     632       188301 :             && op0_const_self
     633       188301 :             && op0 == truthvalue_false_node))
     634       249098 :         *maybe_const_operands &= op1_const;
     635       263668 :       if (!(op0_const
     636       189425 :             && op0_const_self
     637       189425 :             && op0 == truthvalue_false_node))
     638       248752 :         *maybe_const_itself &= op1_const_self;
     639       263668 :       if (!(flag_isoc99
     640       262338 :             && op0_const
     641       188301 :             && op0_const_self
     642       188301 :             && op0 == truthvalue_true_node))
     643       220092 :         *maybe_const_operands &= op2_const;
     644       263668 :       if (!(op0_const
     645       189425 :             && op0_const_self
     646       189425 :             && op0 == truthvalue_true_node))
     647       219977 :         *maybe_const_itself &= op2_const_self;
     648       263668 :       goto out;
     649              : 
     650       101534 :     case VEC_COND_EXPR:
     651       101534 :       orig_op0 = op0 = TREE_OPERAND (expr, 0);
     652       101534 :       orig_op1 = op1 = TREE_OPERAND (expr, 1);
     653       101534 :       orig_op2 = op2 = TREE_OPERAND (expr, 2);
     654       101534 :       op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
     655              :                                    maybe_const_itself, for_int_const, false);
     656       203068 :       STRIP_TYPE_NOPS (op0);
     657       101534 :       op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
     658              :                                    maybe_const_itself, for_int_const, false);
     659       203068 :       STRIP_TYPE_NOPS (op1);
     660       101534 :       op2 = c_fully_fold_internal (op2, in_init, maybe_const_operands,
     661              :                                    maybe_const_itself, for_int_const, false);
     662       203068 :       STRIP_TYPE_NOPS (op2);
     663              : 
     664       101534 :       if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
     665          585 :         ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
     666              :       else
     667       100949 :         ret = fold (expr);
     668       101534 :       goto out;
     669              : 
     670            0 :     case EXCESS_PRECISION_EXPR:
     671              :       /* Each case where an operand with excess precision may be
     672              :          encountered must remove the EXCESS_PRECISION_EXPR around
     673              :          inner operands and possibly put one around the whole
     674              :          expression or possibly convert to the semantic type (which
     675              :          c_fully_fold does); we cannot tell at this stage which is
     676              :          appropriate in any particular case.  */
     677            0 :       gcc_unreachable ();
     678              : 
     679       150768 :     case SAVE_EXPR:
     680              :       /* Make sure to fold the contents of a SAVE_EXPR exactly once.  */
     681       150768 :       op0 = TREE_OPERAND (expr, 0);
     682       150768 :       if (!SAVE_EXPR_FOLDED_P (expr))
     683              :         {
     684        71405 :           op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
     685              :                                        maybe_const_itself, for_int_const,
     686              :                                        false);
     687        71405 :           TREE_OPERAND (expr, 0) = op0;
     688        71405 :           SAVE_EXPR_FOLDED_P (expr) = true;
     689              :         }
     690              :       /* Return the SAVE_EXPR operand if it is invariant.  */
     691       150768 :       if (tree_invariant_p (op0))
     692         3390 :         ret = op0;
     693       150768 :       goto out;
     694              : 
     695         9771 :     default:
     696              :       /* Various codes may appear through folding built-in functions
     697              :          and their arguments.  */
     698         9771 :       goto out;
     699              :     }
     700              : 
     701    264325634 :  out:
     702              :   /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
     703              :      have been done by this point, so remove them again.  */
     704    264325634 :   nowarning |= warning_suppressed_p (ret, OPT_Woverflow);
     705    264501825 :   STRIP_TYPE_NOPS (ret);
     706    264325634 :   if (nowarning && !warning_suppressed_p (ret, OPT_Woverflow))
     707              :     {
     708        12325 :       if (!CAN_HAVE_LOCATION_P (ret))
     709          804 :         ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
     710        12325 :       suppress_warning (ret, OPT_Woverflow);
     711              :     }
     712    264325634 :   if (ret != expr)
     713              :     {
     714     13451422 :       protected_set_expr_location (ret, loc);
     715     13451422 :       if (IS_EXPR_CODE_CLASS (kind))
     716     13451422 :         set_source_range (ret, old_range.m_start, old_range.m_finish);
     717              :     }
     718              :   return ret;
     719              : }
     720              : 
     721              : /* Fold X for consideration by one of the warning functions when checking
     722              :    whether an expression has a constant value.  */
     723              : 
     724              : tree
     725    132280168 : fold_for_warn (tree x)
     726              : {
     727              :   /* The C front-end has already folded X appropriately.  */
     728    132280168 :   return x;
     729              : }
        

Generated by: LCOV version 2.4-beta

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