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 982680 : c_disable_warnings (bool disable)
39 : {
40 0 : if (disable)
41 : {
42 70741 : ++c_inhibit_evaluation_warnings;
43 : }
44 0 : }
45 :
46 : /* If ENABLE is true, re-enable issuing warnings. */
47 :
48 : static void
49 982680 : c_enable_warnings (bool enable)
50 : {
51 0 : if (enable)
52 : {
53 70741 : --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 278821365 : c_fully_fold (tree expr, bool in_init, bool *maybe_const, bool lval)
110 : {
111 278821365 : tree ret;
112 278821365 : tree eptype = NULL_TREE;
113 278821365 : bool dummy = true;
114 278821365 : bool maybe_const_itself = true;
115 278821365 : location_t loc = EXPR_LOCATION (expr);
116 :
117 278821365 : if (!maybe_const)
118 242856051 : maybe_const = &dummy;
119 278821365 : if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
120 : {
121 752 : eptype = TREE_TYPE (expr);
122 752 : expr = TREE_OPERAND (expr, 0);
123 : }
124 278821365 : ret = c_fully_fold_internal (expr, in_init, maybe_const,
125 : &maybe_const_itself, false, lval);
126 278821365 : if (eptype)
127 752 : ret = fold_convert_loc (loc, eptype, ret);
128 278821365 : *maybe_const &= maybe_const_itself;
129 278821365 : 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 501983551 : 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 501983551 : tree ret = expr;
149 501983551 : enum tree_code code = TREE_CODE (expr);
150 501983551 : enum tree_code_class kind = TREE_CODE_CLASS (code);
151 501983551 : location_t loc = EXPR_LOCATION (expr);
152 501983551 : tree op0, op1, op2, op3;
153 501983551 : tree orig_op0, orig_op1, orig_op2;
154 501983551 : bool op0_const = true, op1_const = true, op2_const = true;
155 501983551 : bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
156 501983551 : bool nowarning = warning_suppressed_p (expr, OPT_Woverflow);
157 501983551 : bool unused_p;
158 501983551 : bool op0_lval = false;
159 501983551 : 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 501983551 : if (!IS_EXPR_CODE_CLASS (kind) || kind == tcc_statement)
165 : {
166 : /* Except for variables which we can optimize to its initializer. */
167 235891908 : if (VAR_P (expr) && !lval && (optimize || in_init))
168 : {
169 14545829 : if (in_init)
170 466 : ret = decl_constant_value_1 (expr, true);
171 : else
172 : {
173 14545621 : ret = decl_constant_value (expr);
174 14545621 : if (ret != expr
175 14545621 : && (TYPE_MODE (TREE_TYPE (ret)) == BLKmode
176 15239 : || 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 14543205 : if (ret != expr && TREE_STATIC (expr))
183 5867 : ret = unshare_expr (ret);
184 : return ret;
185 : }
186 : return expr;
187 : }
188 :
189 266091643 : if (IS_EXPR_CODE_CLASS (kind))
190 266091643 : 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 266091643 : if (kind == tcc_vl_exp)
196 : {
197 52208163 : *maybe_const_operands = false;
198 52208163 : ret = fold (expr);
199 52208163 : goto out;
200 : }
201 :
202 213883480 : if (code == C_MAYBE_CONST_EXPR)
203 : {
204 4609544 : tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
205 4609544 : tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
206 4609544 : if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
207 2195459 : *maybe_const_operands = false;
208 4609544 : 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 4609544 : if (pre && !in_init)
215 671 : ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
216 : else
217 : ret = inner;
218 4609544 : 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 209273936 : switch (code)
228 : {
229 4276960 : case MODIFY_EXPR:
230 4276960 : case PREDECREMENT_EXPR:
231 4276960 : case PREINCREMENT_EXPR:
232 4276960 : case POSTDECREMENT_EXPR:
233 4276960 : case POSTINCREMENT_EXPR:
234 4276960 : case COMPOUND_EXPR:
235 4276960 : *maybe_const_operands = false;
236 4276960 : break;
237 :
238 140262 : case VA_ARG_EXPR:
239 140262 : case TARGET_EXPR:
240 140262 : case BIND_EXPR:
241 140262 : case OBJ_TYPE_REF:
242 140262 : *maybe_const_operands = false;
243 140262 : ret = fold (expr);
244 140262 : goto out;
245 :
246 : default:
247 : break;
248 : }
249 :
250 : /* Fold individual tree codes as appropriate. */
251 209133674 : switch (code)
252 : {
253 929908 : 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 929908 : goto out;
257 :
258 2833993 : case COMPONENT_REF:
259 2833993 : orig_op0 = op0 = TREE_OPERAND (expr, 0);
260 2833993 : op1 = TREE_OPERAND (expr, 1);
261 2833993 : op2 = TREE_OPERAND (expr, 2);
262 2833993 : op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
263 : maybe_const_itself, for_int_const, lval);
264 5667986 : STRIP_TYPE_NOPS (op0);
265 2833993 : if (op0 != orig_op0)
266 10401 : ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
267 2833993 : if (ret != expr)
268 : {
269 10401 : TREE_READONLY (ret) = TREE_READONLY (expr);
270 10401 : TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
271 : }
272 2833993 : if (!lval)
273 2058306 : ret = fold (ret);
274 2833993 : goto out;
275 :
276 3014788 : case ARRAY_REF:
277 3014788 : orig_op0 = op0 = TREE_OPERAND (expr, 0);
278 3014788 : orig_op1 = op1 = TREE_OPERAND (expr, 1);
279 3014788 : op2 = TREE_OPERAND (expr, 2);
280 3014788 : op3 = TREE_OPERAND (expr, 3);
281 3014788 : op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
282 : maybe_const_itself, for_int_const, lval);
283 6029576 : STRIP_TYPE_NOPS (op0);
284 3014788 : op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
285 : maybe_const_itself, for_int_const, false);
286 6029576 : STRIP_TYPE_NOPS (op1);
287 : /* Fold "foo"[2] in initializers. */
288 3014788 : 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 3013999 : if (op0 != orig_op0 || op1 != orig_op1)
296 135257 : ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
297 3013999 : if (ret != expr)
298 : {
299 135257 : TREE_READONLY (ret) = TREE_READONLY (expr);
300 135257 : TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
301 135257 : TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
302 : }
303 3013999 : if (!lval)
304 2374401 : ret = fold (ret);
305 3013999 : goto out;
306 :
307 4013913 : case MODIFY_EXPR:
308 4013913 : case PREDECREMENT_EXPR:
309 4013913 : case PREINCREMENT_EXPR:
310 4013913 : case POSTDECREMENT_EXPR:
311 4013913 : case POSTINCREMENT_EXPR:
312 4013913 : op0_lval = true;
313 : /* FALLTHRU */
314 13898485 : case COMPOUND_EXPR:
315 13898485 : case PLUS_EXPR:
316 13898485 : case MINUS_EXPR:
317 13898485 : case MULT_EXPR:
318 13898485 : case POINTER_PLUS_EXPR:
319 13898485 : case POINTER_DIFF_EXPR:
320 13898485 : case TRUNC_DIV_EXPR:
321 13898485 : case CEIL_DIV_EXPR:
322 13898485 : case FLOOR_DIV_EXPR:
323 13898485 : case TRUNC_MOD_EXPR:
324 13898485 : case RDIV_EXPR:
325 13898485 : case EXACT_DIV_EXPR:
326 13898485 : case LSHIFT_EXPR:
327 13898485 : case RSHIFT_EXPR:
328 13898485 : case LROTATE_EXPR:
329 13898485 : case RROTATE_EXPR:
330 13898485 : case BIT_IOR_EXPR:
331 13898485 : case BIT_XOR_EXPR:
332 13898485 : case BIT_AND_EXPR:
333 13898485 : case LT_EXPR:
334 13898485 : case LE_EXPR:
335 13898485 : case GT_EXPR:
336 13898485 : case GE_EXPR:
337 13898485 : case EQ_EXPR:
338 13898485 : case NE_EXPR:
339 13898485 : case COMPLEX_EXPR:
340 13898485 : case TRUTH_AND_EXPR:
341 13898485 : case TRUTH_OR_EXPR:
342 13898485 : case TRUTH_XOR_EXPR:
343 13898485 : case UNORDERED_EXPR:
344 13898485 : case ORDERED_EXPR:
345 13898485 : case UNLT_EXPR:
346 13898485 : case UNLE_EXPR:
347 13898485 : case UNGT_EXPR:
348 13898485 : case UNGE_EXPR:
349 13898485 : case UNEQ_EXPR:
350 13898485 : case MEM_REF:
351 : /* Binary operations evaluating both arguments (increment and
352 : decrement are binary internally in GCC). */
353 13898485 : orig_op0 = op0 = TREE_OPERAND (expr, 0);
354 13898485 : orig_op1 = op1 = TREE_OPERAND (expr, 1);
355 13898485 : op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
356 : maybe_const_itself, for_int_const,
357 : op0_lval);
358 27796970 : 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 13898485 : if (code != MODIFY_EXPR)
362 10843519 : op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
363 : maybe_const_itself, for_int_const, false);
364 13898543 : STRIP_TYPE_NOPS (op1);
365 :
366 13898485 : if (for_int_const && (TREE_CODE (op0) != INTEGER_CST
367 1282 : || TREE_CODE (op1) != INTEGER_CST))
368 273 : goto out;
369 :
370 13898212 : if (TREE_CODE_CLASS (code) == tcc_comparison
371 2513518 : && TREE_CODE (TREE_TYPE (op0)) == NULLPTR_TYPE
372 13898234 : && 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 13898190 : else if (op0 != orig_op0 || op1 != orig_op1 || in_init)
389 2599018 : ret = in_init
390 2600723 : ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
391 2596504 : : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
392 : else
393 11297467 : ret = fold (expr);
394 268319 : 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 13898232 : && !TREE_OVERFLOW_P (op1))
398 19 : overflow_warning (EXPR_LOC_OR_LOC (expr, input_location), ret, expr);
399 13898212 : if (code == LSHIFT_EXPR
400 456087 : && TREE_CODE (orig_op0) != INTEGER_CST
401 370733 : && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
402 892 : || TREE_CODE (TREE_TYPE (orig_op0)) == BITINT_TYPE)
403 370189 : && TREE_CODE (op0) == INTEGER_CST
404 175 : && c_inhibit_evaluation_warnings == 0
405 76 : && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (orig_op0))
406 13898244 : && tree_int_cst_sgn (op0) < 0)
407 6 : warning_at (loc, OPT_Wshift_negative_value,
408 : "left shift of negative value");
409 13898212 : if ((code == LSHIFT_EXPR
410 : || code == RSHIFT_EXPR
411 : || code == LROTATE_EXPR
412 13898212 : || code == RROTATE_EXPR)
413 648251 : && TREE_CODE (orig_op1) != INTEGER_CST
414 146486 : && TREE_CODE (op1) == INTEGER_CST
415 522 : && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
416 13898732 : && c_inhibit_evaluation_warnings == 0)
417 : {
418 330 : if (tree_int_cst_sgn (op1) < 0
419 330 : && !warning_suppressed_p (expr, OPT_Wshift_count_negative))
420 29 : warning_at (loc, OPT_Wshift_count_negative,
421 : (code == LSHIFT_EXPR
422 : ? G_("left shift count is negative")
423 : : G_("right shift count is negative")));
424 313 : else if ((TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
425 106 : || TREE_CODE (TREE_TYPE (orig_op0)) == BITINT_TYPE
426 12 : || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
427 301 : && compare_tree_int (op1,
428 301 : TYPE_PRECISION (TREE_TYPE (orig_op0)))
429 : >= 0
430 328 : && !warning_suppressed_p (expr, OPT_Wshift_count_overflow))
431 6 : warning_at (loc, OPT_Wshift_count_overflow,
432 : (code == LSHIFT_EXPR
433 : ? G_("left shift count >= width of type")
434 : : G_("right shift count >= width of type")));
435 309 : else if (TREE_CODE (TREE_TYPE (orig_op0)) == VECTOR_TYPE
436 321 : && compare_tree_int (op1,
437 12 : TYPE_PRECISION (TREE_TYPE (TREE_TYPE (orig_op0))))
438 : >= 0)
439 9 : warning_at (loc, OPT_Wshift_count_overflow,
440 : code == LSHIFT_EXPR
441 : ? G_("left shift count >= width of vector element")
442 : : G_("right shift count >= width of vector element"));
443 : }
444 13898212 : if (code == LSHIFT_EXPR
445 : /* If either OP0 has been folded to INTEGER_CST... */
446 456087 : && ((TREE_CODE (orig_op0) != INTEGER_CST
447 370733 : && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
448 892 : || TREE_CODE (TREE_TYPE (orig_op0)) == BITINT_TYPE)
449 370189 : && TREE_CODE (op0) == INTEGER_CST)
450 : /* ...or if OP1 has been folded to INTEGER_CST... */
451 455912 : || (TREE_CODE (orig_op1) != INTEGER_CST
452 114781 : && (TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
453 340 : || TREE_CODE (TREE_TYPE (orig_op1)) == BITINT_TYPE)
454 114519 : && TREE_CODE (op1) == INTEGER_CST))
455 13898604 : && c_inhibit_evaluation_warnings == 0)
456 : /* ...then maybe we can detect an overflow. */
457 212 : maybe_warn_shift_overflow (loc, op0, op1);
458 13898212 : if ((code == TRUNC_DIV_EXPR
459 : || code == CEIL_DIV_EXPR
460 13898212 : || code == FLOOR_DIV_EXPR
461 13898212 : || code == EXACT_DIV_EXPR
462 : || code == TRUNC_MOD_EXPR)
463 90708 : && TREE_CODE (orig_op1) != INTEGER_CST
464 12437 : && TREE_CODE (op1) == INTEGER_CST
465 97 : && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
466 0 : || TREE_CODE (TREE_TYPE (orig_op0)) == BITINT_TYPE
467 0 : || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
468 97 : && (TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
469 0 : || TREE_CODE (TREE_TYPE (orig_op1)) == BITINT_TYPE))
470 97 : warn_for_div_by_zero (loc, op1);
471 13898212 : if (code == MEM_REF
472 13898212 : && ret != expr
473 12327 : && TREE_CODE (ret) == MEM_REF)
474 : {
475 12327 : TREE_READONLY (ret) = TREE_READONLY (expr);
476 12327 : TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
477 12327 : TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
478 : }
479 13898212 : goto out;
480 :
481 54754077 : case ADDR_EXPR:
482 54754077 : op0_lval = true;
483 54754077 : goto unary;
484 97903916 : case REALPART_EXPR:
485 97903916 : case IMAGPART_EXPR:
486 97903916 : case VIEW_CONVERT_EXPR:
487 97903916 : op0_lval = lval;
488 : /* FALLTHRU */
489 187477163 : case INDIRECT_REF:
490 187477163 : case FIX_TRUNC_EXPR:
491 187477163 : case FLOAT_EXPR:
492 187477163 : CASE_CONVERT:
493 187477163 : case ADDR_SPACE_CONVERT_EXPR:
494 187477163 : case NON_LVALUE_EXPR:
495 187477163 : case NEGATE_EXPR:
496 187477163 : case BIT_NOT_EXPR:
497 187477163 : case TRUTH_NOT_EXPR:
498 187477163 : case CONJ_EXPR:
499 187477163 : case PAREN_EXPR:
500 187477163 : unary:
501 : /* Unary operations. */
502 187477163 : orig_op0 = op0 = TREE_OPERAND (expr, 0);
503 187477163 : op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
504 : maybe_const_itself, for_int_const,
505 : op0_lval);
506 374954364 : STRIP_TYPE_NOPS (op0);
507 :
508 187477163 : if (for_int_const && TREE_CODE (op0) != INTEGER_CST)
509 14 : goto out;
510 :
511 : /* ??? Cope with user tricks that amount to offsetof. The middle-end is
512 : not prepared to deal with them if they occur in initializers. */
513 187477149 : if (op0 != orig_op0
514 187477149 : && code == ADDR_EXPR
515 509 : && (op1 = get_base_address (op0)) != NULL_TREE
516 509 : && INDIRECT_REF_P (op1)
517 187477312 : && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
518 102 : ret = fold_offsetof (op0, TREE_TYPE (expr));
519 187477047 : else if (op0 != orig_op0 || in_init)
520 4190007 : ret = in_init
521 4190007 : ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
522 1218970 : : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
523 : else
524 183287040 : ret = fold (expr);
525 187477149 : if (code == INDIRECT_REF
526 187477149 : && ret != expr
527 39211 : && INDIRECT_REF_P (ret))
528 : {
529 39113 : TREE_READONLY (ret) = TREE_READONLY (expr);
530 39113 : TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
531 39113 : TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
532 : }
533 187477149 : switch (code)
534 : {
535 : case FIX_TRUNC_EXPR:
536 : case FLOAT_EXPR:
537 : CASE_CONVERT:
538 : /* Don't warn about explicit conversions. We will already
539 : have warned about suspect implicit conversions. */
540 : break;
541 :
542 156805166 : default:
543 156805166 : if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
544 0 : overflow_warning (EXPR_LOCATION (expr), ret, op0);
545 : break;
546 : }
547 187477149 : goto out;
548 :
549 449614 : case TRUTH_ANDIF_EXPR:
550 449614 : case TRUTH_ORIF_EXPR:
551 : /* Binary operations not necessarily evaluating both
552 : arguments. */
553 449614 : orig_op0 = op0 = TREE_OPERAND (expr, 0);
554 449614 : orig_op1 = op1 = TREE_OPERAND (expr, 1);
555 449614 : op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self,
556 : for_int_const, false);
557 899228 : STRIP_TYPE_NOPS (op0);
558 :
559 899228 : unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
560 449614 : ? truthvalue_false_node
561 : : truthvalue_true_node));
562 449614 : c_disable_warnings (unused_p);
563 449614 : op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self,
564 : for_int_const, false);
565 899228 : STRIP_TYPE_NOPS (op1);
566 449614 : c_enable_warnings (unused_p);
567 :
568 449614 : if (for_int_const
569 50 : && (TREE_CODE (op0) != INTEGER_CST
570 : /* Require OP1 be an INTEGER_CST only if it's evaluated. */
571 36 : || (!unused_p && TREE_CODE (op1) != INTEGER_CST)))
572 40 : goto out;
573 :
574 449574 : if (op0 != orig_op0 || op1 != orig_op1 || in_init)
575 439951 : ret = in_init
576 439951 : ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
577 439936 : : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
578 : else
579 9623 : ret = fold (expr);
580 449574 : *maybe_const_operands &= op0_const;
581 449574 : *maybe_const_itself &= op0_const_self;
582 794447 : if (!(flag_isoc99
583 446047 : && op0_const
584 344873 : && op0_const_self
585 : && (code == TRUTH_ANDIF_EXPR
586 128058 : ? op0 == truthvalue_false_node
587 216815 : : op0 == truthvalue_true_node)))
588 438075 : *maybe_const_operands &= op1_const;
589 797223 : if (!(op0_const
590 347649 : && op0_const_self
591 : && (code == TRUTH_ANDIF_EXPR
592 128347 : ? op0 == truthvalue_false_node
593 219302 : : op0 == truthvalue_true_node)))
594 438059 : *maybe_const_itself &= op1_const_self;
595 449574 : goto out;
596 :
597 266533 : case COND_EXPR:
598 266533 : orig_op0 = op0 = TREE_OPERAND (expr, 0);
599 266533 : orig_op1 = op1 = TREE_OPERAND (expr, 1);
600 266533 : orig_op2 = op2 = TREE_OPERAND (expr, 2);
601 266533 : op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self,
602 : for_int_const, false);
603 :
604 533066 : STRIP_TYPE_NOPS (op0);
605 266533 : c_disable_warnings (op0 == truthvalue_false_node);
606 266533 : op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self,
607 : for_int_const, false);
608 533066 : STRIP_TYPE_NOPS (op1);
609 266533 : c_enable_warnings (op0 == truthvalue_false_node);
610 :
611 266533 : c_disable_warnings (op0 == truthvalue_true_node);
612 266533 : op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self,
613 : for_int_const, false);
614 533176 : STRIP_TYPE_NOPS (op2);
615 266533 : c_enable_warnings (op0 == truthvalue_true_node);
616 :
617 266574 : if (for_int_const
618 76 : && (TREE_CODE (op0) != INTEGER_CST
619 : /* Only the evaluated operand must be an INTEGER_CST. */
620 14 : || (op0 == truthvalue_true_node
621 14 : ? TREE_CODE (op1) != INTEGER_CST
622 27 : : TREE_CODE (op2) != INTEGER_CST)))
623 62 : goto out;
624 :
625 266471 : if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
626 210326 : ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
627 : else
628 56145 : ret = fold (expr);
629 266471 : *maybe_const_operands &= op0_const;
630 266471 : *maybe_const_itself &= op0_const_self;
631 266471 : if (!(flag_isoc99
632 265139 : && op0_const
633 190794 : && op0_const_self
634 190794 : && op0 == truthvalue_false_node))
635 251862 : *maybe_const_operands &= op1_const;
636 266471 : if (!(op0_const
637 191919 : && op0_const_self
638 191919 : && op0 == truthvalue_false_node))
639 251516 : *maybe_const_itself &= op1_const_self;
640 266471 : if (!(flag_isoc99
641 265139 : && op0_const
642 190794 : && op0_const_self
643 190794 : && op0 == truthvalue_true_node))
644 222865 : *maybe_const_operands &= op2_const;
645 266471 : if (!(op0_const
646 191919 : && op0_const_self
647 191919 : && op0 == truthvalue_true_node))
648 222750 : *maybe_const_itself &= op2_const_self;
649 266471 : goto out;
650 :
651 102131 : case VEC_COND_EXPR:
652 102131 : orig_op0 = op0 = TREE_OPERAND (expr, 0);
653 102131 : orig_op1 = op1 = TREE_OPERAND (expr, 1);
654 102131 : orig_op2 = op2 = TREE_OPERAND (expr, 2);
655 102131 : op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
656 : maybe_const_itself, for_int_const, false);
657 204262 : STRIP_TYPE_NOPS (op0);
658 102131 : op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
659 : maybe_const_itself, for_int_const, false);
660 204262 : STRIP_TYPE_NOPS (op1);
661 102131 : op2 = c_fully_fold_internal (op2, in_init, maybe_const_operands,
662 : maybe_const_itself, for_int_const, false);
663 204262 : STRIP_TYPE_NOPS (op2);
664 :
665 102131 : if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
666 589 : ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
667 : else
668 101542 : ret = fold (expr);
669 102131 : goto out;
670 :
671 0 : case EXCESS_PRECISION_EXPR:
672 : /* Each case where an operand with excess precision may be
673 : encountered must remove the EXCESS_PRECISION_EXPR around
674 : inner operands and possibly put one around the whole
675 : expression or possibly convert to the semantic type (which
676 : c_fully_fold does); we cannot tell at this stage which is
677 : appropriate in any particular case. */
678 0 : gcc_unreachable ();
679 :
680 151274 : case SAVE_EXPR:
681 : /* Make sure to fold the contents of a SAVE_EXPR exactly once. */
682 151274 : op0 = TREE_OPERAND (expr, 0);
683 151274 : if (!SAVE_EXPR_FOLDED_P (expr))
684 : {
685 71725 : op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
686 : maybe_const_itself, for_int_const,
687 : false);
688 71725 : TREE_OPERAND (expr, 0) = op0;
689 71725 : SAVE_EXPR_FOLDED_P (expr) = true;
690 : }
691 : /* Return the SAVE_EXPR operand if it is invariant. */
692 151274 : if (tree_invariant_p (op0))
693 3398 : ret = op0;
694 151274 : goto out;
695 :
696 9785 : default:
697 : /* Various codes may appear through folding built-in functions
698 : and their arguments. */
699 9785 : goto out;
700 : }
701 :
702 266091643 : out:
703 : /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
704 : have been done by this point, so remove them again. */
705 266091643 : nowarning |= warning_suppressed_p (ret, OPT_Woverflow);
706 266268824 : STRIP_TYPE_NOPS (ret);
707 266091643 : if (nowarning && !warning_suppressed_p (ret, OPT_Woverflow))
708 : {
709 12482 : if (!CAN_HAVE_LOCATION_P (ret))
710 924 : ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
711 12482 : suppress_warning (ret, OPT_Woverflow);
712 : }
713 266091643 : if (ret != expr)
714 : {
715 13536668 : protected_set_expr_location (ret, loc);
716 13536668 : if (IS_EXPR_CODE_CLASS (kind))
717 13536668 : set_source_range (ret, old_range.m_start, old_range.m_finish);
718 : }
719 : return ret;
720 : }
721 :
722 : /* Fold X for consideration by one of the warning functions when checking
723 : whether an expression has a constant value. */
724 :
725 : tree
726 133109529 : fold_for_warn (tree x)
727 : {
728 : /* The C front-end has already folded X appropriately. */
729 133109529 : return x;
730 : }
|