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