Line data Source code
1 : /* C++-specific tree lowering bits; see also c-gimplify.cc and gimple.cc.
2 :
3 : Copyright (C) 2002-2026 Free Software Foundation, Inc.
4 : Contributed by Jason Merrill <jason@redhat.com>
5 :
6 : This file is part of GCC.
7 :
8 : GCC is free software; you can redistribute it and/or modify it under
9 : the terms of the GNU General Public License as published by the Free
10 : Software Foundation; either version 3, or (at your option) any later
11 : version.
12 :
13 : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 : WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 : for more details.
17 :
18 : You should have received a copy of the GNU General Public License
19 : along with GCC; see the file COPYING3. If not see
20 : <http://www.gnu.org/licenses/>. */
21 :
22 : #include "config.h"
23 : #include "system.h"
24 : #include "coretypes.h"
25 : #include "target.h"
26 : #include "basic-block.h"
27 : #include "cp-tree.h"
28 : #include "gimple.h"
29 : #include "predict.h"
30 : #include "stor-layout.h"
31 : #include "tree-iterator.h"
32 : #include "gimplify.h"
33 : #include "c-family/c-ubsan.h"
34 : #include "stringpool.h"
35 : #include "attribs.h"
36 : #include "asan.h"
37 : #include "gcc-rich-location.h"
38 : #include "memmodel.h"
39 : #include "tm_p.h"
40 : #include "output.h"
41 : #include "file-prefix-map.h"
42 : #include "cgraph.h"
43 : #include "omp-general.h"
44 : #include "opts.h"
45 : #include "gcc-urlifier.h"
46 : #include "contracts.h" // build_contract_check ()
47 : #include "builtins.h"
48 :
49 : /* Keep track of forward references to immediate-escalating functions in
50 : case they become consteval. This vector contains ADDR_EXPRs and
51 : PTRMEM_CSTs; it also stores FUNCTION_DECLs that had an escalating
52 : function call in them, to check that they can be evaluated to a constant,
53 : and immediate-escalating functions that may become consteval. */
54 : static GTY(()) hash_set<tree> *deferred_escalating_exprs;
55 :
56 : static void
57 26268152 : remember_escalating_expr (tree t)
58 : {
59 26268152 : if (uses_template_parms (t))
60 : /* Templates don't escalate, and cp_fold_immediate can get confused by
61 : other template trees in the function body (c++/115986). */
62 : return;
63 26268152 : if (!deferred_escalating_exprs)
64 19572 : deferred_escalating_exprs = hash_set<tree>::create_ggc (37);
65 26268152 : deferred_escalating_exprs->add (t);
66 : }
67 :
68 : /* Flags for cp_fold and cp_fold_r. */
69 :
70 : enum fold_flags {
71 : ff_none = 0,
72 : /* Whether we're being called from cp_fold_function. */
73 : ff_genericize = 1 << 0,
74 : /* Whether we're folding a point where we know we're
75 : definitely not in a manifestly constant-evaluated
76 : context. */
77 : ff_mce_false = 1 << 1,
78 : /* Whether we're only folding non-ODR usages of constants.
79 : This happens before saving the constexpr funcdef, so
80 : we should do as little other folding as possible.
81 : Mutually exclusive with ff_mce_false. */
82 : ff_only_non_odr = 1 << 2,
83 : };
84 :
85 : using fold_flags_t = int;
86 :
87 84279878 : struct cp_fold_data
88 : {
89 : hash_set<tree> pset;
90 : fold_flags_t flags;
91 198542541 : cp_fold_data (fold_flags_t flags): flags (flags)
92 : {
93 198542541 : gcc_checking_assert (!(flags & ff_mce_false)
94 : || !(flags & ff_only_non_odr));
95 198542541 : }
96 78663613 : mce_value mce ()
97 : {
98 78663613 : return flags & ff_mce_false ? mce_false : mce_unknown;
99 : }
100 : };
101 :
102 : /* Forward declarations. */
103 :
104 : static tree cp_genericize_r (tree *, int *, void *);
105 : static tree cp_fold_r (tree *, int *, void *);
106 : static void cp_genericize_tree (tree*, bool);
107 : static tree cp_fold (tree, fold_flags_t);
108 : static tree cp_fold_immediate_r (tree *, int *, void *);
109 :
110 : /* Genericize a TRY_BLOCK. */
111 :
112 : static void
113 20303 : genericize_try_block (tree *stmt_p)
114 : {
115 20303 : tree body = TRY_STMTS (*stmt_p);
116 20303 : tree cleanup = TRY_HANDLERS (*stmt_p);
117 :
118 20303 : *stmt_p = build2 (TRY_CATCH_EXPR, void_type_node, body, cleanup);
119 20303 : }
120 :
121 : /* Genericize a HANDLER by converting to a CATCH_EXPR. */
122 :
123 : static void
124 23479 : genericize_catch_block (tree *stmt_p)
125 : {
126 23479 : tree type = HANDLER_TYPE (*stmt_p);
127 23479 : tree body = HANDLER_BODY (*stmt_p);
128 :
129 : /* FIXME should the caught type go in TREE_TYPE? */
130 23479 : *stmt_p = build2 (CATCH_EXPR, void_type_node, type, body);
131 23479 : }
132 :
133 : /* A terser interface for building a representation of an exception
134 : specification. */
135 :
136 : static tree
137 4491 : build_gimple_eh_filter_tree (tree body, tree allowed, tree failure)
138 : {
139 4491 : tree t;
140 :
141 : /* FIXME should the allowed types go in TREE_TYPE? */
142 4491 : t = build2 (EH_FILTER_EXPR, void_type_node, allowed, NULL_TREE);
143 4491 : append_to_statement_list (failure, &EH_FILTER_FAILURE (t));
144 :
145 4491 : t = build2 (TRY_CATCH_EXPR, void_type_node, NULL_TREE, t);
146 4491 : append_to_statement_list (body, &TREE_OPERAND (t, 0));
147 :
148 4491 : return t;
149 : }
150 :
151 : /* Genericize an EH_SPEC_BLOCK by converting it to a
152 : TRY_CATCH_EXPR/EH_FILTER_EXPR pair. */
153 :
154 : static void
155 4491 : genericize_eh_spec_block (tree *stmt_p)
156 : {
157 4491 : tree body = EH_SPEC_STMTS (*stmt_p);
158 4491 : tree allowed = EH_SPEC_RAISES (*stmt_p);
159 4491 : tree failure = build_call_n (call_unexpected_fn, 1, build_exc_ptr ());
160 :
161 4491 : *stmt_p = build_gimple_eh_filter_tree (body, allowed, failure);
162 4491 : suppress_warning (*stmt_p);
163 4491 : suppress_warning (TREE_OPERAND (*stmt_p, 1));
164 4491 : }
165 :
166 : /* Return the first non-compound statement in STMT. */
167 :
168 : tree
169 15204380 : first_stmt (tree stmt)
170 : {
171 22923211 : switch (TREE_CODE (stmt))
172 : {
173 6102241 : case STATEMENT_LIST:
174 6102241 : if (tree_statement_list_node *p = STATEMENT_LIST_HEAD (stmt))
175 4050702 : return first_stmt (p->stmt);
176 2051539 : return void_node;
177 :
178 3668129 : case BIND_EXPR:
179 3668129 : return first_stmt (BIND_EXPR_BODY (stmt));
180 :
181 : default:
182 : return stmt;
183 : }
184 : }
185 :
186 : /* Genericize an IF_STMT by turning it into a COND_EXPR. */
187 :
188 : static void
189 22336773 : genericize_if_stmt (tree *stmt_p)
190 : {
191 22336773 : tree stmt, cond, then_, else_;
192 22336773 : location_t locus = EXPR_LOCATION (*stmt_p);
193 :
194 22336773 : stmt = *stmt_p;
195 22336773 : cond = IF_COND (stmt);
196 22336773 : then_ = THEN_CLAUSE (stmt);
197 22336773 : else_ = ELSE_CLAUSE (stmt);
198 :
199 22336773 : if (then_ && else_)
200 : {
201 7602190 : tree ft = first_stmt (then_);
202 7602190 : tree fe = first_stmt (else_);
203 7602190 : br_predictor pr;
204 7602190 : if (TREE_CODE (ft) == PREDICT_EXPR
205 43758 : && TREE_CODE (fe) == PREDICT_EXPR
206 48 : && (pr = PREDICT_EXPR_PREDICTOR (ft)) == PREDICT_EXPR_PREDICTOR (fe)
207 7602229 : && (pr == PRED_HOT_LABEL || pr == PRED_COLD_LABEL))
208 : {
209 3 : gcc_rich_location richloc (EXPR_LOC_OR_LOC (ft, locus));
210 3 : richloc.add_range (EXPR_LOC_OR_LOC (fe, locus));
211 3 : warning_at (&richloc, OPT_Wattributes,
212 : "both branches of %<if%> statement marked as %qs",
213 : pr == PRED_HOT_LABEL ? "likely" : "unlikely");
214 3 : }
215 : }
216 :
217 22336773 : if (IF_STMT_VACUOUS_INIT_P (stmt))
218 : {
219 58 : gcc_checking_assert (integer_zerop (cond));
220 58 : gcc_checking_assert (!else_ || !TREE_SIDE_EFFECTS (else_));
221 58 : tree lab = create_artificial_label (UNKNOWN_LOCATION);
222 58 : VACUOUS_INIT_LABEL_P (lab) = 1;
223 58 : tree goto_expr = build_stmt (UNKNOWN_LOCATION, GOTO_EXPR, lab);
224 58 : tree label_expr = build_stmt (UNKNOWN_LOCATION, LABEL_EXPR, lab);
225 58 : if (TREE_CODE (then_) == STATEMENT_LIST)
226 : {
227 58 : tree_stmt_iterator i = tsi_start (then_);
228 58 : tsi_link_before (&i, goto_expr, TSI_CONTINUE_LINKING);
229 58 : i = tsi_last (then_);
230 58 : tsi_link_after (&i, label_expr, TSI_CONTINUE_LINKING);
231 58 : stmt = then_;
232 : }
233 : else
234 : {
235 0 : stmt = NULL_TREE;
236 0 : append_to_statement_list (goto_expr, &stmt);
237 0 : append_to_statement_list (then_, &stmt);
238 0 : append_to_statement_list (label_expr, &stmt);
239 : }
240 58 : *stmt_p = stmt;
241 58 : return;
242 : }
243 :
244 22336715 : if (!then_)
245 2010 : then_ = build_empty_stmt (locus);
246 22336715 : if (!else_)
247 14732624 : else_ = build_empty_stmt (locus);
248 :
249 : /* consteval if has been verified not to have the then_/else_ blocks
250 : entered by gotos/case labels from elsewhere, and as then_ block
251 : can contain unfolded immediate function calls, we have to discard
252 : the then_ block regardless of whether else_ has side-effects or not. */
253 22336715 : if (IF_STMT_CONSTEVAL_P (stmt))
254 : {
255 79325 : if (block_may_fallthru (then_))
256 25572 : stmt = build3 (COND_EXPR, void_type_node, boolean_false_node,
257 : void_node, else_);
258 : else
259 53753 : stmt = else_;
260 : }
261 22257390 : else if (IF_STMT_CONSTEXPR_P (stmt))
262 7330850 : stmt = integer_nonzerop (cond) ? then_ : else_;
263 : /* ??? This optimization doesn't seem to belong here, but removing it
264 : causes -Wreturn-type regressions (e.g. 107310). */
265 17277822 : else if (integer_nonzerop (cond) && !TREE_SIDE_EFFECTS (else_))
266 99922 : stmt = then_;
267 17177900 : else if (integer_zerop (cond) && !TREE_SIDE_EFFECTS (then_))
268 35443 : stmt = else_;
269 : else
270 17142457 : stmt = build3 (COND_EXPR, void_type_node, cond, then_, else_);
271 22336715 : protected_set_expr_location_if_unset (stmt, locus);
272 22336715 : *stmt_p = stmt;
273 : }
274 :
275 : /* Hook into the middle of gimplifying an OMP_FOR node. */
276 :
277 : static enum gimplify_status
278 45626 : cp_gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
279 : {
280 45626 : tree for_stmt = *expr_p;
281 45626 : gimple_seq seq = NULL;
282 :
283 : /* Protect ourselves from recursion. */
284 45626 : if (OMP_FOR_GIMPLIFYING_P (for_stmt))
285 : return GS_UNHANDLED;
286 21206 : OMP_FOR_GIMPLIFYING_P (for_stmt) = 1;
287 :
288 21206 : gimplify_and_add (for_stmt, &seq);
289 21206 : gimple_seq_add_seq (pre_p, seq);
290 :
291 21206 : OMP_FOR_GIMPLIFYING_P (for_stmt) = 0;
292 :
293 21206 : return GS_ALL_DONE;
294 : }
295 :
296 : /* Gimplify an EXPR_STMT node. */
297 :
298 : static void
299 4098312 : gimplify_expr_stmt (tree *stmt_p)
300 : {
301 4098312 : tree stmt = EXPR_STMT_EXPR (*stmt_p);
302 :
303 4098312 : if (stmt == error_mark_node)
304 : stmt = NULL;
305 :
306 : /* Gimplification of a statement expression will nullify the
307 : statement if all its side effects are moved to *PRE_P and *POST_P.
308 :
309 : In this case we will not want to emit the gimplified statement.
310 : However, we may still want to emit a warning, so we do that before
311 : gimplification. */
312 4094307 : if (stmt && warn_unused_value)
313 : {
314 308363 : if (!TREE_SIDE_EFFECTS (stmt))
315 : {
316 0 : if (!IS_EMPTY_STMT (stmt)
317 6846 : && !VOID_TYPE_P (TREE_TYPE (stmt))
318 6846 : && !warning_suppressed_p (stmt, OPT_Wunused_value))
319 0 : warning (OPT_Wunused_value, "statement with no effect");
320 : }
321 : else
322 301517 : warn_if_unused_value (stmt, input_location);
323 : }
324 :
325 4098312 : if (stmt == NULL_TREE)
326 4005 : stmt = alloc_stmt_list ();
327 :
328 4098312 : *stmt_p = stmt;
329 4098312 : }
330 :
331 : /* Gimplify initialization from an AGGR_INIT_EXPR. */
332 :
333 : static void
334 11984574 : cp_gimplify_init_expr (tree *expr_p)
335 : {
336 11984574 : tree from = TREE_OPERAND (*expr_p, 1);
337 11984574 : tree to = TREE_OPERAND (*expr_p, 0);
338 11984574 : tree t;
339 :
340 11984574 : if (TREE_CODE (from) == TARGET_EXPR)
341 213567 : if (tree init = TARGET_EXPR_INITIAL (from))
342 : {
343 : /* Make sure that we expected to elide this temporary. But also allow
344 : gimplify_modify_expr_rhs to elide temporaries of trivial type. */
345 213567 : gcc_checking_assert (TARGET_EXPR_ELIDING_P (from)
346 : || !TREE_ADDRESSABLE (TREE_TYPE (from)));
347 213567 : if (target_expr_needs_replace (from))
348 : {
349 : /* If this was changed by cp_genericize_target_expr, we need to
350 : walk into it to replace uses of the slot. */
351 96 : replace_decl (&init, TARGET_EXPR_SLOT (from), to);
352 96 : *expr_p = init;
353 96 : return;
354 : }
355 : else
356 : from = init;
357 : }
358 :
359 : /* Look through any COMPOUND_EXPRs, since build_compound_expr pushes them
360 : inside the TARGET_EXPR. */
361 12071448 : for (t = from; t; )
362 : {
363 12071448 : tree sub = TREE_CODE (t) == COMPOUND_EXPR ? TREE_OPERAND (t, 0) : t;
364 :
365 : /* If we are initializing from an AGGR_INIT_EXPR, drop the INIT_EXPR and
366 : replace the slot operand with our target.
367 :
368 : Should we add a target parm to gimplify_expr instead? No, as in this
369 : case we want to replace the INIT_EXPR. */
370 12071448 : if (TREE_CODE (sub) == AGGR_INIT_EXPR
371 12071448 : || TREE_CODE (sub) == VEC_INIT_EXPR)
372 : {
373 115756 : if (TREE_CODE (sub) == AGGR_INIT_EXPR)
374 115756 : AGGR_INIT_EXPR_SLOT (sub) = to;
375 : else
376 0 : VEC_INIT_EXPR_SLOT (sub) = to;
377 115756 : *expr_p = from;
378 :
379 : /* The initialization is now a side-effect, so the container can
380 : become void. */
381 115756 : if (from != sub)
382 73 : TREE_TYPE (from) = void_type_node;
383 : }
384 :
385 : /* Handle aggregate NSDMI. */
386 12071448 : replace_placeholders (sub, to);
387 :
388 12071448 : if (t == sub)
389 : break;
390 : else
391 86970 : t = TREE_OPERAND (t, 1);
392 : }
393 :
394 : }
395 :
396 : /* Gimplify a MUST_NOT_THROW_EXPR. */
397 :
398 : static enum gimplify_status
399 577929 : gimplify_must_not_throw_expr (tree *expr_p, gimple_seq *pre_p)
400 : {
401 577929 : tree stmt = *expr_p;
402 577929 : tree temp = voidify_wrapper_expr (stmt, NULL);
403 577929 : tree body = TREE_OPERAND (stmt, 0);
404 577929 : gimple_seq try_ = NULL;
405 577929 : gimple_seq catch_ = NULL;
406 577929 : gimple *mnt;
407 :
408 577929 : gimplify_and_add (body, &try_);
409 577929 : mnt = gimple_build_eh_must_not_throw (call_terminate_fn);
410 577929 : gimple_seq_add_stmt_without_update (&catch_, mnt);
411 577929 : mnt = gimple_build_try (try_, catch_, GIMPLE_TRY_CATCH);
412 :
413 577929 : gimple_seq_add_stmt_without_update (pre_p, mnt);
414 577929 : if (temp)
415 : {
416 33 : *expr_p = temp;
417 33 : return GS_OK;
418 : }
419 :
420 : *expr_p = NULL;
421 : return GS_ALL_DONE;
422 : }
423 :
424 : /* Return TRUE if an operand (OP) of a given TYPE being copied is
425 : really just an empty class copy.
426 :
427 : Check that the operand has a simple form so that TARGET_EXPRs and
428 : non-empty CONSTRUCTORs get reduced properly, and we leave the
429 : return slot optimization alone because it isn't a copy. */
430 :
431 : bool
432 20063409 : simple_empty_class_p (tree type, tree op, tree_code code)
433 : {
434 23471230 : if (TREE_CODE (op) == COMPOUND_EXPR)
435 183960 : return simple_empty_class_p (type, TREE_OPERAND (op, 1), code);
436 4303028 : if (SIMPLE_TARGET_EXPR_P (op)
437 26512683 : && TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
438 : /* The TARGET_EXPR is itself a simple copy, look through it. */
439 3223861 : return simple_empty_class_p (type, TARGET_EXPR_INITIAL (op), code);
440 :
441 20063409 : if (TREE_CODE (op) == PARM_DECL
442 20063409 : && TREE_ADDRESSABLE (TREE_TYPE (op)))
443 : {
444 9 : tree fn = DECL_CONTEXT (op);
445 9 : if (DECL_THUNK_P (fn)
446 12 : || lambda_static_thunk_p (fn))
447 : /* In a thunk, we pass through invisible reference parms, so this isn't
448 : actually a copy. */
449 : return false;
450 : }
451 :
452 20063400 : return
453 20063400 : (TREE_CODE (op) == EMPTY_CLASS_EXPR
454 20063370 : || code == MODIFY_EXPR
455 17008722 : || is_gimple_lvalue (op)
456 13444019 : || INDIRECT_REF_P (op)
457 13016450 : || (TREE_CODE (op) == CONSTRUCTOR
458 2893950 : && CONSTRUCTOR_NELTS (op) == 0)
459 10418996 : || (TREE_CODE (op) == CALL_EXPR
460 2739950 : && !CALL_EXPR_RETURN_SLOT_OPT (op)))
461 12274272 : && !TREE_CLOBBER_P (op)
462 31723788 : && is_really_empty_class (type, /*ignore_vptr*/true);
463 : }
464 :
465 : /* Returns true if evaluating E as an lvalue has side-effects;
466 : specifically, a volatile lvalue has TREE_SIDE_EFFECTS, but it doesn't really
467 : have side-effects until there is a read or write through it. */
468 :
469 : static bool
470 2716550 : lvalue_has_side_effects (tree e)
471 : {
472 2716550 : if (!TREE_SIDE_EFFECTS (e))
473 : return false;
474 60533 : while (handled_component_p (e))
475 : {
476 6465 : if (TREE_CODE (e) == ARRAY_REF
477 6465 : && TREE_SIDE_EFFECTS (TREE_OPERAND (e, 1)))
478 : return true;
479 4627 : e = TREE_OPERAND (e, 0);
480 : }
481 54068 : if (DECL_P (e))
482 : /* Just naming a variable has no side-effects. */
483 : return false;
484 37062 : else if (INDIRECT_REF_P (e))
485 : /* Similarly, indirection has no side-effects. */
486 36925 : return TREE_SIDE_EFFECTS (TREE_OPERAND (e, 0));
487 : else
488 : /* For anything else, trust TREE_SIDE_EFFECTS. */
489 137 : return TREE_SIDE_EFFECTS (e);
490 : }
491 :
492 : /* Return true if FN is an immediate-escalating function. */
493 :
494 : bool
495 203594087 : immediate_escalating_function_p (tree fn)
496 : {
497 203594087 : if (!fn || !flag_immediate_escalation)
498 : return false;
499 :
500 203593619 : gcc_checking_assert (TREE_CODE (fn) == FUNCTION_DECL);
501 :
502 203593619 : if (DECL_IMMEDIATE_FUNCTION_P (fn))
503 : return false;
504 :
505 : /* An immediate-escalating function is
506 : -- the call operator of a lambda that is not declared with the consteval
507 : specifier */
508 207328766 : if (LAMBDA_FUNCTION_P (fn))
509 : return true;
510 : /* -- a non-user-provided defaulted function that is not declared with the
511 : consteval specifier */
512 201482538 : if (DECL_DEFAULTED_FN (fn) && !user_provided_p (fn))
513 : return true;
514 : /* -- a function that results from the instantiation of a templated entity
515 : defined with the constexpr specifier. */
516 193657787 : return is_instantiation_of_constexpr (fn);
517 : }
518 :
519 : /* Return true if FN is an immediate-escalating function that has not been
520 : checked for escalating expressions.. */
521 :
522 : static bool
523 203590713 : unchecked_immediate_escalating_function_p (tree fn)
524 : {
525 203590713 : return (immediate_escalating_function_p (fn)
526 203590713 : && !DECL_ESCALATION_CHECKED_P (fn));
527 : }
528 :
529 : /* Promote FN to an immediate function, including its clones. */
530 :
531 : void
532 2338 : promote_function_to_consteval (tree fn)
533 : {
534 2338 : SET_DECL_IMMEDIATE_FUNCTION_P (fn);
535 2338 : DECL_ESCALATION_CHECKED_P (fn) = true;
536 2338 : tree clone;
537 4830 : FOR_EACH_CLONE (clone, fn)
538 : {
539 2492 : SET_DECL_IMMEDIATE_FUNCTION_P (clone);
540 2492 : DECL_ESCALATION_CHECKED_P (clone) = true;
541 : }
542 2338 : }
543 :
544 : /* A wrapper around cp_fold_immediate_r. Return a non-null tree if
545 : we found a non-constant immediate function, or taking the address
546 : of an immediate function. */
547 :
548 : tree
549 16313877 : cp_fold_immediate (tree *tp, mce_value manifestly_const_eval,
550 : tree decl /*= current_function_decl*/)
551 : {
552 16313877 : if (cxx_dialect <= cxx17)
553 : return NULL_TREE;
554 :
555 16306499 : temp_override<tree> cfd (current_function_decl, decl);
556 :
557 16306499 : fold_flags_t flags = ff_none;
558 16306499 : if (manifestly_const_eval == mce_false)
559 9576969 : flags |= ff_mce_false;
560 :
561 16306499 : cp_fold_data data (flags);
562 16306499 : int save_errorcount = errorcount;
563 16306499 : tree r = cp_walk_tree (tp, cp_fold_immediate_r, &data, NULL);
564 16306499 : if (errorcount > save_errorcount)
565 49 : return integer_one_node;
566 : return r;
567 16306499 : }
568 :
569 : /* Maybe say that FN (a function decl with DECL_IMMEDIATE_FUNCTION_P set)
570 : was initially not an immediate function, but was promoted to one because
571 : its body contained an immediate-escalating expression or conversion. */
572 :
573 : static void
574 548 : maybe_explain_promoted_consteval (location_t loc, tree fn)
575 : {
576 548 : if (DECL_ESCALATION_CHECKED_P (fn))
577 : {
578 : /* See if we can figure out what made the function consteval. */
579 130 : tree x = cp_fold_immediate (&DECL_SAVED_TREE (fn), mce_unknown, NULL_TREE);
580 130 : if (x)
581 103 : inform (cp_expr_loc_or_loc (x, loc),
582 : "%qD was promoted to an immediate function because its "
583 : "body contains an immediate-escalating expression %qE", fn, x);
584 : else
585 27 : inform (loc, "%qD was promoted to an immediate function", fn);
586 : }
587 548 : }
588 :
589 : /* Gimplify *EXPR_P as rvalue into an expression that can't be modified
590 : by expressions with side-effects in other operands. */
591 :
592 : static enum gimplify_status
593 38922 : gimplify_to_rvalue (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
594 : bool (*gimple_test_f) (tree))
595 : {
596 38922 : enum gimplify_status t
597 38922 : = gimplify_expr (expr_p, pre_p, post_p, gimple_test_f, fb_rvalue);
598 38922 : if (t == GS_ERROR)
599 : return GS_ERROR;
600 38919 : else if (is_gimple_variable (*expr_p) && TREE_CODE (*expr_p) != SSA_NAME)
601 2957 : *expr_p = get_initialized_tmp_var (*expr_p, pre_p);
602 : return t;
603 : }
604 :
605 : /* Like gimplify_arg, but if ORDERED is set (which should be set if
606 : any of the arguments this argument is sequenced before has
607 : TREE_SIDE_EFFECTS set, make sure expressions with is_gimple_reg_type type
608 : are gimplified into SSA_NAME or a fresh temporary and for
609 : non-is_gimple_reg_type we don't optimize away TARGET_EXPRs. */
610 :
611 : static enum gimplify_status
612 4348957 : cp_gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location,
613 : bool ordered)
614 : {
615 4348957 : enum gimplify_status t;
616 4348957 : if (ordered
617 433810 : && !is_gimple_reg_type (TREE_TYPE (*arg_p))
618 4350858 : && TREE_CODE (*arg_p) == TARGET_EXPR)
619 : {
620 : /* gimplify_arg would strip away the TARGET_EXPR, but
621 : that can mean we don't copy the argument and some following
622 : argument with side-effect could modify it. */
623 1756 : protected_set_expr_location (*arg_p, call_location);
624 1756 : return gimplify_expr (arg_p, pre_p, NULL, is_gimple_lvalue, fb_either);
625 : }
626 : else
627 : {
628 4347201 : t = gimplify_arg (arg_p, pre_p, call_location);
629 4347201 : if (t == GS_ERROR)
630 : return GS_ERROR;
631 4347201 : else if (ordered
632 432054 : && is_gimple_reg_type (TREE_TYPE (*arg_p))
633 431909 : && is_gimple_variable (*arg_p)
634 219316 : && TREE_CODE (*arg_p) != SSA_NAME
635 : /* No need to force references into register, references
636 : can't be modified. */
637 133822 : && !TYPE_REF_P (TREE_TYPE (*arg_p))
638 : /* And this can't be modified either. */
639 4436797 : && *arg_p != current_class_ptr)
640 8786 : *arg_p = get_initialized_tmp_var (*arg_p, pre_p);
641 : return t;
642 : }
643 :
644 : }
645 :
646 : /* Emit a decl = {CLOBBER(bob)}; stmt before DECL_EXPR or first
647 : TARGET_EXPR gimplification for -flifetime-dse=2. */
648 :
649 : static void
650 1495655 : maybe_emit_clobber_object_begin (tree decl, gimple_seq *pre_p)
651 : {
652 1495655 : if (VAR_P (decl)
653 1495056 : && auto_var_p (decl)
654 1459787 : && TREE_TYPE (decl) != error_mark_node
655 1459769 : && DECL_NONTRIVIALLY_INITIALIZED_P (decl)
656 : /* Don't do it if it is fully initialized. */
657 902799 : && DECL_INITIAL (decl) == NULL_TREE
658 452766 : && !DECL_HAS_VALUE_EXPR_P (decl)
659 451818 : && !OPAQUE_TYPE_P (TREE_TYPE (decl))
660 : /* Nor going to have decl = .DEFERRED_INIT (...); added. */
661 1947469 : && (flag_auto_var_init == AUTO_INIT_UNINITIALIZED
662 90138 : || lookup_attribute ("uninitialized", DECL_ATTRIBUTES (decl))
663 90138 : || lookup_attribute ("indeterminate", DECL_ATTRIBUTES (decl))))
664 : {
665 361677 : tree eltype = strip_array_types (TREE_TYPE (decl));
666 361677 : if (RECORD_OR_UNION_TYPE_P (eltype)
667 361677 : && !is_empty_class (eltype))
668 : {
669 151799 : tree clobber
670 151799 : = build_clobber (TREE_TYPE (decl), CLOBBER_OBJECT_BEGIN);
671 151799 : gimple *g = gimple_build_assign (decl, clobber);
672 151799 : gimple_set_location (g, DECL_SOURCE_LOCATION (decl));
673 151799 : gimple_seq_add_stmt_without_update (pre_p, g);
674 : }
675 : }
676 1495655 : }
677 :
678 : /* Do C++-specific gimplification. Args are as for gimplify_expr. */
679 :
680 : int
681 177273373 : cp_gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
682 : {
683 177273373 : int saved_stmts_are_full_exprs_p = 0;
684 177273373 : location_t loc = cp_expr_loc_or_input_loc (*expr_p);
685 177273373 : enum tree_code code = TREE_CODE (*expr_p);
686 177273373 : enum gimplify_status ret;
687 :
688 177273373 : if (STATEMENT_CODE_P (code))
689 : {
690 4146585 : saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
691 8293170 : current_stmt_tree ()->stmts_are_full_exprs_p
692 4146585 : = STMT_IS_FULL_EXPR_P (*expr_p);
693 : }
694 :
695 177273373 : switch (code)
696 : {
697 368938 : case AGGR_INIT_EXPR:
698 368938 : simplify_aggr_init_expr (expr_p);
699 368938 : ret = GS_OK;
700 368938 : break;
701 :
702 0 : case VEC_INIT_EXPR:
703 0 : {
704 0 : *expr_p = expand_vec_init_expr (NULL_TREE, *expr_p,
705 : tf_warning_or_error);
706 :
707 0 : cp_fold_data data (ff_genericize | ff_mce_false);
708 0 : cp_walk_tree (expr_p, cp_fold_r, &data, NULL);
709 0 : cp_genericize_tree (expr_p, false);
710 0 : copy_if_shared (expr_p);
711 0 : ret = GS_OK;
712 0 : }
713 0 : break;
714 :
715 22617 : case THROW_EXPR:
716 : /* FIXME communicate throw type to back end, probably by moving
717 : THROW_EXPR into ../tree.def. */
718 22617 : *expr_p = TREE_OPERAND (*expr_p, 0);
719 22617 : ret = GS_OK;
720 22617 : break;
721 :
722 577929 : case MUST_NOT_THROW_EXPR:
723 577929 : ret = gimplify_must_not_throw_expr (expr_p, pre_p);
724 577929 : break;
725 :
726 : /* We used to do this for MODIFY_EXPR as well, but that's unsafe; the
727 : LHS of an assignment might also be involved in the RHS, as in bug
728 : 25979. */
729 11984574 : case INIT_EXPR:
730 11984574 : cp_gimplify_init_expr (expr_p);
731 11984574 : if (TREE_CODE (*expr_p) != INIT_EXPR)
732 : return GS_OK;
733 : /* Fall through. */
734 16087694 : case MODIFY_EXPR:
735 11868722 : modify_expr_case:
736 16087694 : {
737 : /* If the back end isn't clever enough to know that the lhs and rhs
738 : types are the same, add an explicit conversion. */
739 16087694 : tree op0 = TREE_OPERAND (*expr_p, 0);
740 16087694 : tree op1 = TREE_OPERAND (*expr_p, 1);
741 :
742 16087694 : if (!error_operand_p (op0)
743 16087694 : && !error_operand_p (op1)
744 16087675 : && (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (op0))
745 16084629 : || TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (op1)))
746 16090749 : && !useless_type_conversion_p (TREE_TYPE (op1), TREE_TYPE (op0)))
747 9 : TREE_OPERAND (*expr_p, 1) = build1 (VIEW_CONVERT_EXPR,
748 9 : TREE_TYPE (op0), op1);
749 :
750 16087685 : else if (simple_empty_class_p (TREE_TYPE (op0), op1, code))
751 : {
752 218977 : while (TREE_CODE (op1) == TARGET_EXPR)
753 : /* We're disconnecting the initializer from its target,
754 : don't create a temporary. */
755 7124 : op1 = TARGET_EXPR_INITIAL (op1);
756 :
757 : /* Remove any copies of empty classes. Also drop volatile
758 : variables on the RHS to avoid infinite recursion from
759 : gimplify_expr trying to load the value. */
760 211853 : if (TREE_SIDE_EFFECTS (op1))
761 : {
762 16213 : if (TREE_THIS_VOLATILE (op1)
763 0 : && (REFERENCE_CLASS_P (op1) || DECL_P (op1)))
764 0 : op1 = build_fold_addr_expr (op1);
765 :
766 16213 : suppress_warning (op1, OPT_Wunused_result);
767 16213 : gimplify_and_add (op1, pre_p);
768 : }
769 211853 : gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
770 : is_gimple_lvalue, fb_lvalue);
771 211853 : *expr_p = TREE_OPERAND (*expr_p, 0);
772 211853 : if (code == RETURN_EXPR && REFERENCE_CLASS_P (*expr_p))
773 : /* Avoid 'return *<retval>;' */
774 1267 : *expr_p = TREE_OPERAND (*expr_p, 0);
775 : }
776 : /* P0145 says that the RHS is sequenced before the LHS.
777 : gimplify_modify_expr gimplifies the RHS before the LHS, but that
778 : isn't quite strong enough in two cases:
779 :
780 : 1) gimplify.cc wants to leave a CALL_EXPR on the RHS, which would
781 : mean it's evaluated after the LHS.
782 :
783 : 2) the value calculation of the RHS is also sequenced before the
784 : LHS, so for scalar assignment we need to preevaluate if the
785 : RHS could be affected by LHS side-effects even if it has no
786 : side-effects of its own. We don't need this for classes because
787 : class assignment takes its RHS by reference. */
788 15875832 : else if (flag_strong_eval_order > 1
789 14543561 : && TREE_CODE (*expr_p) == MODIFY_EXPR
790 2716550 : && lvalue_has_side_effects (op0)
791 15914586 : && (TREE_CODE (op1) == CALL_EXPR
792 31748 : || (SCALAR_TYPE_P (TREE_TYPE (op1))
793 25215 : && !TREE_CONSTANT (op1))))
794 21727 : TREE_OPERAND (*expr_p, 1) = get_initialized_tmp_var (op1, pre_p);
795 : }
796 : ret = GS_OK;
797 : break;
798 :
799 100171 : case EMPTY_CLASS_EXPR:
800 : /* We create an empty CONSTRUCTOR with RECORD_TYPE. */
801 100171 : *expr_p = build_constructor (TREE_TYPE (*expr_p), NULL);
802 100171 : ret = GS_OK;
803 100171 : break;
804 :
805 0 : case BASELINK:
806 0 : *expr_p = BASELINK_FUNCTIONS (*expr_p);
807 0 : ret = GS_OK;
808 0 : break;
809 :
810 20303 : case TRY_BLOCK:
811 20303 : genericize_try_block (expr_p);
812 20303 : ret = GS_OK;
813 20303 : break;
814 :
815 23479 : case HANDLER:
816 23479 : genericize_catch_block (expr_p);
817 23479 : ret = GS_OK;
818 23479 : break;
819 :
820 4491 : case EH_SPEC_BLOCK:
821 4491 : genericize_eh_spec_block (expr_p);
822 4491 : ret = GS_OK;
823 4491 : break;
824 :
825 0 : case USING_STMT:
826 0 : gcc_unreachable ();
827 :
828 0 : case FOR_STMT:
829 0 : case WHILE_STMT:
830 0 : case DO_STMT:
831 0 : case SWITCH_STMT:
832 0 : case CONTINUE_STMT:
833 0 : case BREAK_STMT:
834 0 : gcc_unreachable ();
835 :
836 45626 : case OMP_FOR:
837 45626 : case OMP_SIMD:
838 45626 : case OMP_DISTRIBUTE:
839 45626 : case OMP_LOOP:
840 45626 : case OMP_TASKLOOP:
841 45626 : case OMP_TILE:
842 45626 : case OMP_UNROLL:
843 45626 : ret = cp_gimplify_omp_for (expr_p, pre_p);
844 45626 : break;
845 :
846 4098312 : case EXPR_STMT:
847 4098312 : gimplify_expr_stmt (expr_p);
848 4098312 : ret = GS_OK;
849 4098312 : break;
850 :
851 0 : case UNARY_PLUS_EXPR:
852 0 : {
853 0 : tree arg = TREE_OPERAND (*expr_p, 0);
854 0 : tree type = TREE_TYPE (*expr_p);
855 0 : *expr_p = (TREE_TYPE (arg) != type) ? fold_convert (type, arg)
856 : : arg;
857 0 : ret = GS_OK;
858 : }
859 0 : break;
860 :
861 8870558 : case CALL_EXPR:
862 8870558 : ret = GS_OK;
863 8870558 : if (flag_strong_eval_order == 2
864 8400765 : && CALL_EXPR_FN (*expr_p)
865 8020785 : && !CALL_EXPR_OPERATOR_SYNTAX (*expr_p)
866 15990516 : && cp_get_callee_fndecl_nofold (*expr_p) == NULL_TREE)
867 : {
868 38922 : tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
869 38922 : enum gimplify_status t
870 38922 : = gimplify_to_rvalue (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
871 : is_gimple_call_addr);
872 38922 : if (t == GS_ERROR)
873 : ret = GS_ERROR;
874 : /* GIMPLE considers most pointer conversion useless, but for
875 : calls we actually care about the exact function pointer type. */
876 38919 : else if (TREE_TYPE (CALL_EXPR_FN (*expr_p)) != fnptrtype)
877 9013 : CALL_EXPR_FN (*expr_p)
878 18026 : = build1 (NOP_EXPR, fnptrtype, CALL_EXPR_FN (*expr_p));
879 : }
880 8870558 : if (!CALL_EXPR_FN (*expr_p))
881 : /* Internal function call. */
882 402699 : switch (CALL_EXPR_IFN (*expr_p))
883 : {
884 36 : case IFN_BSWAP:
885 36 : case IFN_BITREVERSE:
886 36 : if (ret == GS_OK)
887 : {
888 36 : location_t loc = EXPR_LOCATION (*expr_p);
889 36 : internal_fn ifn = CALL_EXPR_IFN (*expr_p);
890 36 : tree arg = CALL_EXPR_ARG (*expr_p, 0);
891 36 : tree r = fold_build_builtin_bswapg_bitreverseg (loc, ifn,
892 : arg);
893 36 : if (TREE_CODE (r) == CALL_EXPR
894 27 : && !CALL_EXPR_FN (r)
895 36 : && CALL_EXPR_IFN (r) == ifn)
896 : break;
897 36 : *expr_p = r;
898 36 : return ret;
899 : }
900 : break;
901 246 : case IFN_CLMUL:
902 246 : if (ret == GS_OK)
903 : {
904 246 : location_t loc = EXPR_LOCATION (*expr_p);
905 246 : tree arg1 = CALL_EXPR_ARG (*expr_p, 0);
906 246 : tree arg2 = CALL_EXPR_ARG (*expr_p, 1);
907 246 : tree arg3 = NULL_TREE;
908 246 : if (call_expr_nargs (*expr_p) == 3)
909 72 : arg3 = CALL_EXPR_ARG (*expr_p, 2);
910 246 : tree r = fold_build_builtin_clmulg (loc, arg1, arg2, arg3);
911 246 : if (TREE_CODE (r) == CALL_EXPR
912 144 : && !CALL_EXPR_FN (r)
913 390 : && CALL_EXPR_IFN (r) == IFN_CLMUL)
914 : break;
915 102 : *expr_p = r;
916 102 : return ret;
917 : }
918 : break;
919 : default:
920 : break;
921 : }
922 8467859 : else if (CALL_EXPR_REVERSE_ARGS (*expr_p))
923 : {
924 : /* This is a call to a (compound) assignment operator that used
925 : the operator syntax; gimplify the RHS first. */
926 49356 : gcc_assert (call_expr_nargs (*expr_p) == 2);
927 49356 : gcc_assert (!CALL_EXPR_ORDERED_ARGS (*expr_p));
928 49356 : enum gimplify_status t
929 49356 : = cp_gimplify_arg (&CALL_EXPR_ARG (*expr_p, 1), pre_p, loc,
930 49356 : TREE_SIDE_EFFECTS (CALL_EXPR_ARG (*expr_p, 0)));
931 49356 : if (t == GS_ERROR)
932 : ret = GS_ERROR;
933 : }
934 8418503 : else if (CALL_EXPR_ORDERED_ARGS (*expr_p))
935 : {
936 : /* Leave the last argument for gimplify_call_expr, to avoid problems
937 : with __builtin_va_arg_pack(). */
938 313253 : int nargs = call_expr_nargs (*expr_p) - 1;
939 313253 : int last_side_effects_arg = -1;
940 617977 : for (int i = nargs; i > 0; --i)
941 331782 : if (TREE_SIDE_EFFECTS (CALL_EXPR_ARG (*expr_p, i)))
942 : {
943 : last_side_effects_arg = i;
944 : break;
945 : }
946 656155 : for (int i = 0; i < nargs; ++i)
947 : {
948 342902 : enum gimplify_status t
949 342902 : = cp_gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p, loc,
950 : i < last_side_effects_arg);
951 342902 : if (t == GS_ERROR)
952 0 : ret = GS_ERROR;
953 : }
954 : }
955 8105250 : else if (flag_strong_eval_order
956 8105250 : && !CALL_EXPR_OPERATOR_SYNTAX (*expr_p))
957 : {
958 : /* If flag_strong_eval_order, evaluate the object argument first. */
959 7502910 : tree fntype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
960 7502910 : if (INDIRECT_TYPE_P (fntype))
961 7502907 : fntype = TREE_TYPE (fntype);
962 7502910 : tree decl = cp_get_callee_fndecl_nofold (*expr_p);
963 : /* We can't just rely on 'decl' because virtual function callees
964 : are expressed as OBJ_TYPE_REF. Note that the xobj memfn check
965 : will also hold for calls of the form (&A::f)(a, ...) which does
966 : not require such sequencing, though it's allowed under
967 : "indeterminately sequenced". */
968 7502910 : if (TREE_CODE (fntype) == METHOD_TYPE
969 7502910 : || (decl && DECL_LANG_SPECIFIC (decl)
970 3533771 : && DECL_XOBJ_MEMBER_FUNCTION_P (decl)))
971 : {
972 3956699 : int nargs = call_expr_nargs (*expr_p);
973 3956699 : bool side_effects = false;
974 5580750 : for (int i = 1; i < nargs; ++i)
975 2014051 : if (TREE_SIDE_EFFECTS (CALL_EXPR_ARG (*expr_p, i)))
976 : {
977 : side_effects = true;
978 : break;
979 : }
980 3956699 : enum gimplify_status t
981 3956699 : = cp_gimplify_arg (&CALL_EXPR_ARG (*expr_p, 0), pre_p, loc,
982 : side_effects);
983 3956699 : if (t == GS_ERROR)
984 : ret = GS_ERROR;
985 : }
986 : }
987 8870420 : if (ret != GS_ERROR)
988 : {
989 8870417 : tree decl = cp_get_callee_fndecl_nofold (*expr_p);
990 8870417 : if (!decl)
991 : break;
992 8423380 : if (fndecl_built_in_p (decl, BUILT_IN_FRONTEND))
993 81 : switch (DECL_FE_FUNCTION_CODE (decl))
994 : {
995 0 : case CP_BUILT_IN_IS_CONSTANT_EVALUATED:
996 0 : *expr_p = boolean_false_node;
997 0 : break;
998 0 : case CP_BUILT_IN_SOURCE_LOCATION:
999 0 : *expr_p
1000 0 : = fold_builtin_source_location (*expr_p);
1001 0 : break;
1002 0 : case CP_BUILT_IN_IS_CORRESPONDING_MEMBER:
1003 0 : *expr_p
1004 0 : = fold_builtin_is_corresponding_member
1005 0 : (EXPR_LOCATION (*expr_p), call_expr_nargs (*expr_p),
1006 : &CALL_EXPR_ARG (*expr_p, 0));
1007 0 : break;
1008 0 : case CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS:
1009 0 : *expr_p
1010 0 : = fold_builtin_is_pointer_inverconvertible_with_class
1011 0 : (EXPR_LOCATION (*expr_p), call_expr_nargs (*expr_p),
1012 : &CALL_EXPR_ARG (*expr_p, 0));
1013 0 : break;
1014 0 : case CP_BUILT_IN_EH_PTR_ADJUST_REF:
1015 0 : error_at (EXPR_LOCATION (*expr_p),
1016 : "%qs used outside of constant expressions",
1017 : "__builtin_eh_ptr_adjust_ref");
1018 0 : *expr_p = void_node;
1019 0 : break;
1020 9 : case CP_BUILT_IN_CURRENT_EXCEPTION:
1021 9 : case CP_BUILT_IN_UNCAUGHT_EXCEPTIONS:
1022 9 : {
1023 9 : const char *name
1024 : = (DECL_FE_FUNCTION_CODE (decl)
1025 : == CP_BUILT_IN_CURRENT_EXCEPTION
1026 9 : ? "current_exception" : "uncaught_exceptions");
1027 9 : tree newdecl = lookup_qualified_name (std_node, name);
1028 9 : if (error_operand_p (newdecl))
1029 0 : *expr_p = build_zero_cst (TREE_TYPE (*expr_p));
1030 9 : else if (TREE_CODE (newdecl) != FUNCTION_DECL
1031 9 : || !same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1032 : TREE_TYPE (TREE_TYPE (decl)))
1033 18 : || (TYPE_ARG_TYPES (TREE_TYPE (newdecl))
1034 9 : != void_list_node))
1035 : {
1036 0 : error_at (EXPR_LOCATION (*expr_p),
1037 : "unexpected %<std::%s%> declaration",
1038 : name);
1039 0 : *expr_p = build_zero_cst (TREE_TYPE (*expr_p));
1040 : }
1041 : else
1042 9 : *expr_p = build_call_expr_loc (EXPR_LOCATION (*expr_p),
1043 : newdecl, 0);
1044 : break;
1045 : }
1046 0 : case CP_BUILT_IN_IS_STRING_LITERAL:
1047 0 : *expr_p
1048 0 : = fold_builtin_is_string_literal (EXPR_LOCATION (*expr_p),
1049 0 : call_expr_nargs (*expr_p),
1050 : &CALL_EXPR_ARG (*expr_p,
1051 : 0));
1052 0 : break;
1053 0 : case CP_BUILT_IN_CONSTEXPR_DIAG:
1054 0 : *expr_p = void_node;
1055 0 : break;
1056 72 : case CP_BUILT_IN_START_LIFETIME:
1057 72 : *expr_p = fold_convert (void_type_node,
1058 : CALL_EXPR_ARG (*expr_p, 0));
1059 72 : break;
1060 : default:
1061 : break;
1062 : }
1063 8423299 : else if (fndecl_built_in_p (decl, BUILT_IN_CLZG, BUILT_IN_CTZG))
1064 33 : ret = (enum gimplify_status) c_gimplify_expr (expr_p, pre_p,
1065 : post_p);
1066 : else
1067 : /* All consteval functions should have been processed by now. */
1068 8423266 : gcc_checking_assert (!immediate_invocation_p (decl));
1069 : }
1070 : break;
1071 :
1072 700785 : case TARGET_EXPR:
1073 : /* A TARGET_EXPR that expresses direct-initialization should have been
1074 : elided by cp_gimplify_init_expr. */
1075 700785 : gcc_checking_assert (!TARGET_EXPR_DIRECT_INIT_P (*expr_p));
1076 : /* Likewise, but allow extra temps of trivial type so that
1077 : gimplify_init_ctor_preeval can materialize subobjects of a CONSTRUCTOR
1078 : on the rhs of an assignment, as in constexpr-aggr1.C. */
1079 700785 : gcc_checking_assert (!TARGET_EXPR_ELIDING_P (*expr_p)
1080 : || !TREE_ADDRESSABLE (TREE_TYPE (*expr_p)));
1081 700785 : if (flag_lifetime_dse > 1
1082 700657 : && TARGET_EXPR_INITIAL (*expr_p)
1083 1392414 : && VOID_TYPE_P (TREE_TYPE (TARGET_EXPR_INITIAL (*expr_p))))
1084 254858 : maybe_emit_clobber_object_begin (TARGET_EXPR_SLOT (*expr_p), pre_p);
1085 : ret = GS_UNHANDLED;
1086 : break;
1087 :
1088 3 : case PTRMEM_CST:
1089 3 : *expr_p = cplus_expand_constant (*expr_p);
1090 3 : if (TREE_CODE (*expr_p) == PTRMEM_CST)
1091 : ret = GS_ERROR;
1092 : else
1093 177157383 : ret = GS_OK;
1094 : break;
1095 :
1096 1241025 : case DECL_EXPR:
1097 1241025 : if (flag_lifetime_dse > 1)
1098 1240797 : maybe_emit_clobber_object_begin (DECL_EXPR_DECL (*expr_p), pre_p);
1099 : ret = GS_UNHANDLED;
1100 : break;
1101 :
1102 1232468 : case RETURN_EXPR:
1103 1232468 : if (TREE_OPERAND (*expr_p, 0)
1104 1232468 : && (TREE_CODE (TREE_OPERAND (*expr_p, 0)) == INIT_EXPR
1105 12822 : || TREE_CODE (TREE_OPERAND (*expr_p, 0)) == MODIFY_EXPR))
1106 : {
1107 1164324 : expr_p = &TREE_OPERAND (*expr_p, 0);
1108 : /* Avoid going through the INIT_EXPR case, which can
1109 : degrade INIT_EXPRs into AGGR_INIT_EXPRs. */
1110 1164324 : goto modify_expr_case;
1111 : }
1112 : /* Fall through. */
1113 :
1114 144995590 : default:
1115 144995590 : ret = (enum gimplify_status) c_gimplify_expr (expr_p, pre_p, post_p);
1116 144995590 : break;
1117 : }
1118 :
1119 : /* Restore saved state. */
1120 177157383 : if (STATEMENT_CODE_P (code))
1121 4146585 : current_stmt_tree ()->stmts_are_full_exprs_p
1122 4146585 : = saved_stmts_are_full_exprs_p;
1123 :
1124 : return ret;
1125 : }
1126 :
1127 : bool
1128 2040143636 : is_invisiref_parm (const_tree t)
1129 : {
1130 1912505515 : return ((TREE_CODE (t) == PARM_DECL || TREE_CODE (t) == RESULT_DECL)
1131 2085066843 : && DECL_BY_REFERENCE (t));
1132 : }
1133 :
1134 : /* A stable comparison routine for use with splay trees and DECLs. */
1135 :
1136 : static int
1137 63164 : splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
1138 : {
1139 63164 : tree a = (tree) xa;
1140 63164 : tree b = (tree) xb;
1141 :
1142 63164 : return DECL_UID (a) - DECL_UID (b);
1143 : }
1144 :
1145 : /* OpenMP context during genericization. */
1146 :
1147 : struct cp_genericize_omp_taskreg
1148 : {
1149 : bool is_parallel;
1150 : bool default_shared;
1151 : struct cp_genericize_omp_taskreg *outer;
1152 : splay_tree variables;
1153 : };
1154 :
1155 : /* Return true if genericization should try to determine if
1156 : DECL is firstprivate or shared within task regions. */
1157 :
1158 : static bool
1159 120013 : omp_var_to_track (tree decl)
1160 : {
1161 120013 : tree type = TREE_TYPE (decl);
1162 120013 : if (is_invisiref_parm (decl))
1163 537 : type = TREE_TYPE (type);
1164 119476 : else if (TYPE_REF_P (type))
1165 4317 : type = TREE_TYPE (type);
1166 145072 : while (TREE_CODE (type) == ARRAY_TYPE)
1167 25059 : type = TREE_TYPE (type);
1168 120013 : if (type == error_mark_node || !CLASS_TYPE_P (type))
1169 : return false;
1170 14269 : if (VAR_P (decl) && CP_DECL_THREAD_LOCAL_P (decl))
1171 : return false;
1172 14266 : if (cxx_omp_predetermined_sharing (decl) != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
1173 57 : return false;
1174 : return true;
1175 : }
1176 :
1177 : /* Note DECL use in OpenMP region OMP_CTX during genericization. */
1178 :
1179 : static void
1180 14475 : omp_cxx_notice_variable (struct cp_genericize_omp_taskreg *omp_ctx, tree decl)
1181 : {
1182 14475 : splay_tree_node n = splay_tree_lookup (omp_ctx->variables,
1183 : (splay_tree_key) decl);
1184 14475 : if (n == NULL)
1185 : {
1186 4375 : int flags = OMP_CLAUSE_DEFAULT_SHARED;
1187 4375 : if (omp_ctx->outer)
1188 1281 : omp_cxx_notice_variable (omp_ctx->outer, decl);
1189 4375 : if (!omp_ctx->default_shared)
1190 : {
1191 980 : struct cp_genericize_omp_taskreg *octx;
1192 :
1193 1105 : for (octx = omp_ctx->outer; octx; octx = octx->outer)
1194 : {
1195 934 : n = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
1196 934 : if (n && n->value != OMP_CLAUSE_DEFAULT_SHARED)
1197 : {
1198 : flags = OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
1199 : break;
1200 : }
1201 891 : if (octx->is_parallel)
1202 : break;
1203 : }
1204 980 : if (octx == NULL
1205 980 : && (TREE_CODE (decl) == PARM_DECL
1206 124 : || (!(TREE_STATIC (decl) || DECL_EXTERNAL (decl))
1207 41 : && DECL_CONTEXT (decl) == current_function_decl)))
1208 : flags = OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
1209 892 : if (flags == OMP_CLAUSE_DEFAULT_FIRSTPRIVATE)
1210 : {
1211 : /* DECL is implicitly determined firstprivate in
1212 : the current task construct. Ensure copy ctor and
1213 : dtor are instantiated, because during gimplification
1214 : it will be already too late. */
1215 131 : tree type = TREE_TYPE (decl);
1216 131 : if (is_invisiref_parm (decl))
1217 2 : type = TREE_TYPE (type);
1218 129 : else if (TYPE_REF_P (type))
1219 56 : type = TREE_TYPE (type);
1220 181 : while (TREE_CODE (type) == ARRAY_TYPE)
1221 50 : type = TREE_TYPE (type);
1222 131 : get_copy_ctor (type, tf_none);
1223 131 : get_dtor (type, tf_none);
1224 : }
1225 : }
1226 4375 : splay_tree_insert (omp_ctx->variables, (splay_tree_key) decl, flags);
1227 : }
1228 14475 : }
1229 :
1230 : /* True if any of the element initializers in CTOR are TARGET_EXPRs that are
1231 : not expected to elide, e.g. because unsafe_copy_elision_p is true. */
1232 :
1233 : static bool
1234 134081 : any_non_eliding_target_exprs (tree ctor)
1235 : {
1236 621671 : for (const constructor_elt &e : *CONSTRUCTOR_ELTS (ctor))
1237 : {
1238 487593 : if (TREE_CODE (e.value) == TARGET_EXPR
1239 487593 : && !TARGET_EXPR_ELIDING_P (e.value))
1240 : return true;
1241 : }
1242 : return false;
1243 : }
1244 :
1245 : /* If we might need to clean up a partially constructed object, break down the
1246 : CONSTRUCTOR with split_nonconstant_init. Also expand VEC_INIT_EXPR at this
1247 : point. If initializing TO with FROM is non-trivial, overwrite *REPLACE with
1248 : the result. */
1249 :
1250 : static void
1251 83472352 : cp_genericize_init (tree *replace, tree from, tree to, vec<tree,va_gc>** flags)
1252 : {
1253 83472352 : tree init = NULL_TREE;
1254 83472352 : if (TREE_CODE (from) == VEC_INIT_EXPR)
1255 1005 : init = expand_vec_init_expr (to, from, tf_warning_or_error, flags);
1256 83471347 : else if (TREE_CODE (from) == CONSTRUCTOR
1257 4916134 : && TREE_SIDE_EFFECTS (from)
1258 83606774 : && ((flag_exceptions
1259 135340 : && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (from)))
1260 134081 : || any_non_eliding_target_exprs (from)))
1261 : {
1262 1349 : to = cp_stabilize_reference (to);
1263 1349 : replace_placeholders (from, to);
1264 1349 : init = split_nonconstant_init (to, from);
1265 : }
1266 :
1267 2354 : if (init)
1268 : {
1269 2354 : if (*replace == from)
1270 : /* Make cp_gimplify_init_expr call replace_decl on this
1271 : TARGET_EXPR_INITIAL. */
1272 724 : init = fold_convert (void_type_node, init);
1273 2354 : *replace = init;
1274 : }
1275 83472352 : }
1276 :
1277 : /* For an INIT_EXPR, replace the INIT_EXPR itself. */
1278 :
1279 : static void
1280 62767749 : cp_genericize_init_expr (tree *stmt_p)
1281 : {
1282 62767749 : iloc_sentinel ils = EXPR_LOCATION (*stmt_p);
1283 62767749 : tree to = TREE_OPERAND (*stmt_p, 0);
1284 62767749 : tree from = TREE_OPERAND (*stmt_p, 1);
1285 6578912 : if (SIMPLE_TARGET_EXPR_P (from)
1286 : /* Return gets confused if we clobber its INIT_EXPR this soon. */
1287 67674241 : && TREE_CODE (to) != RESULT_DECL)
1288 174520 : from = TARGET_EXPR_INITIAL (from);
1289 62767749 : cp_genericize_init (stmt_p, from, to, nullptr);
1290 62767749 : }
1291 :
1292 : /* For a TARGET_EXPR, change the TARGET_EXPR_INITIAL. We will need to use
1293 : replace_decl later when we know what we're initializing. */
1294 :
1295 : static void
1296 20704603 : cp_genericize_target_expr (tree *stmt_p)
1297 : {
1298 20704603 : iloc_sentinel ils = EXPR_LOCATION (*stmt_p);
1299 20704603 : tree slot = TARGET_EXPR_SLOT (*stmt_p);
1300 20704603 : vec<tree, va_gc> *flags = make_tree_vector ();
1301 20704603 : cp_genericize_init (&TARGET_EXPR_INITIAL (*stmt_p),
1302 20704603 : TARGET_EXPR_INITIAL (*stmt_p), slot, &flags);
1303 20704603 : gcc_assert (!DECL_INITIAL (slot));
1304 62113841 : for (tree f : flags)
1305 : {
1306 : /* Once initialization is complete TARGET_EXPR_CLEANUP becomes active, so
1307 : disable any subobject cleanups. */
1308 32 : tree d = build_disable_temp_cleanup (f);
1309 32 : auto &r = TARGET_EXPR_INITIAL (*stmt_p);
1310 32 : r = add_stmt_to_compound (r, d);
1311 : }
1312 20704603 : release_tree_vector (flags);
1313 20704603 : }
1314 :
1315 : /* Similar to if (target_expr_needs_replace) replace_decl, but TP is the
1316 : TARGET_EXPR_INITIAL, and this also updates *_SLOT. We need this extra
1317 : replacement when cp_folding TARGET_EXPR to preserve the invariant that
1318 : AGGR_INIT_EXPR_SLOT agrees with the enclosing TARGET_EXPR_SLOT. */
1319 :
1320 : static bool
1321 130 : maybe_replace_decl (tree *tp, tree decl, tree replacement)
1322 : {
1323 130 : if (!*tp || !VOID_TYPE_P (TREE_TYPE (*tp)))
1324 : return false;
1325 50 : tree t = *tp;
1326 50 : while (TREE_CODE (t) == COMPOUND_EXPR)
1327 0 : t = TREE_OPERAND (t, 1);
1328 50 : if (TREE_CODE (t) == AGGR_INIT_EXPR)
1329 50 : replace_decl (&AGGR_INIT_EXPR_SLOT (t), decl, replacement);
1330 0 : else if (TREE_CODE (t) == VEC_INIT_EXPR)
1331 0 : replace_decl (&VEC_INIT_EXPR_SLOT (t), decl, replacement);
1332 : else
1333 0 : replace_decl (tp, decl, replacement);
1334 : return true;
1335 : }
1336 :
1337 : /* Genericization context. */
1338 :
1339 51479064 : struct cp_genericize_data
1340 : {
1341 : hash_set<tree> *p_set;
1342 : auto_vec<tree> bind_expr_stack;
1343 : struct cp_genericize_omp_taskreg *omp_ctx;
1344 : tree try_block;
1345 : bool no_sanitize_p;
1346 : bool handle_invisiref_parm_p;
1347 : };
1348 :
1349 : /* Emit an error about taking the address of an immediate function.
1350 : EXPR is the whole expression; DECL is the immediate function. */
1351 :
1352 : static void
1353 71 : taking_address_of_imm_fn_error (tree expr, tree decl)
1354 : {
1355 71 : auto_diagnostic_group d;
1356 71 : const location_t loc = (TREE_CODE (expr) == PTRMEM_CST
1357 71 : ? PTRMEM_CST_LOCATION (expr)
1358 71 : : EXPR_LOCATION (expr));
1359 71 : error_at (loc, "taking address of an immediate function %qD", decl);
1360 71 : maybe_explain_promoted_consteval (loc, decl);
1361 71 : }
1362 :
1363 : /* Build up an INIT_EXPR to initialize the object of a constructor call that
1364 : has been folded to a constant value. CALL is the CALL_EXPR for the
1365 : constructor call; INIT is the value. */
1366 :
1367 : static tree
1368 489 : cp_build_init_expr_for_ctor (tree call, tree init)
1369 : {
1370 489 : tree a = CALL_EXPR_ARG (call, 0);
1371 489 : if (is_dummy_object (a))
1372 : return init;
1373 489 : const bool return_this = targetm.cxx.cdtor_returns_this ();
1374 489 : const location_t loc = EXPR_LOCATION (call);
1375 489 : if (return_this)
1376 0 : a = cp_save_expr (a);
1377 489 : tree s = build_fold_indirect_ref_loc (loc, a);
1378 489 : init = cp_build_init_expr (s, init);
1379 489 : if (return_this)
1380 : {
1381 0 : init = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (call), init,
1382 0 : fold_convert_loc (loc, TREE_TYPE (call), a));
1383 0 : suppress_warning (init);
1384 : }
1385 : return init;
1386 : }
1387 :
1388 : /* A walk_tree callback for cp_fold_function and cp_fully_fold_init to handle
1389 : immediate functions. */
1390 :
1391 : static tree
1392 3230044430 : cp_fold_immediate_r (tree *stmt_p, int *walk_subtrees, void *data_)
1393 : {
1394 3230044430 : auto data = static_cast<cp_fold_data *>(data_);
1395 3230044430 : tree stmt = *stmt_p;
1396 : /* The purpose of this is not to emit errors for mce_unknown. */
1397 3230044430 : const tsubst_flags_t complain = (data->flags & ff_mce_false
1398 3230044430 : ? tf_error : tf_none);
1399 3230044430 : const tree_code code = TREE_CODE (stmt);
1400 :
1401 : /* No need to look into types or unevaluated operands.
1402 : NB: This affects cp_fold_r as well. */
1403 3230044430 : if (TYPE_P (stmt)
1404 3227628419 : || unevaluated_p (code)
1405 : /* We do not use in_immediate_context here because it checks
1406 : more than is desirable, e.g., sk_template_parms. */
1407 3227281230 : || cp_unevaluated_operand
1408 6457325660 : || (current_function_decl
1409 6275634866 : && DECL_IMMEDIATE_FUNCTION_P (current_function_decl)))
1410 : {
1411 2783646 : *walk_subtrees = 0;
1412 2783646 : return NULL_TREE;
1413 : }
1414 :
1415 3227260784 : tree decl = NULL_TREE;
1416 3227260784 : bool call_p = false;
1417 :
1418 : /* We are looking for &fn or fn(). */
1419 3227260784 : switch (code)
1420 : {
1421 36732947 : case DECL_EXPR:
1422 : /* Clear consteval-only DECL_EXPRs. */
1423 36732947 : if (flag_reflection)
1424 : {
1425 771985 : tree d = DECL_EXPR_DECL (stmt);
1426 771985 : if (VAR_P (d) && consteval_only_p (d))
1427 884 : *stmt_p = void_node;
1428 : }
1429 : break;
1430 155697497 : case CALL_EXPR:
1431 155697497 : case AGGR_INIT_EXPR:
1432 155697497 : if (tree fn = cp_get_callee (stmt))
1433 155383579 : if (TREE_CODE (fn) != ADDR_EXPR || ADDR_EXPR_DENOTES_CALL_P (fn))
1434 148641045 : decl = cp_get_fndecl_from_callee (fn, /*fold*/false);
1435 : call_p = true;
1436 : break;
1437 32985 : case PTRMEM_CST:
1438 32985 : decl = PTRMEM_CST_MEMBER (stmt);
1439 32985 : break;
1440 235760004 : case ADDR_EXPR:
1441 235760004 : if (!ADDR_EXPR_DENOTES_CALL_P (stmt))
1442 88822127 : decl = TREE_OPERAND (stmt, 0);
1443 : break;
1444 21867848 : case IF_STMT:
1445 21867848 : if (IF_STMT_CONSTEVAL_P (stmt))
1446 : {
1447 79340 : if (!data->pset.add (stmt))
1448 79340 : cp_walk_tree (&ELSE_CLAUSE (stmt), cp_fold_immediate_r, data_,
1449 : nullptr);
1450 79340 : *walk_subtrees = 0;
1451 79340 : return NULL_TREE;
1452 : }
1453 : /* FALLTHRU */
1454 2798958011 : default:
1455 2798958011 : if (data->pset.add (stmt))
1456 478510495 : *walk_subtrees = 0;
1457 : return NULL_TREE;
1458 : }
1459 :
1460 237497041 : if (!decl || TREE_CODE (decl) != FUNCTION_DECL)
1461 : return NULL_TREE;
1462 :
1463 : /* Fully escalate once all templates have been instantiated. What we're
1464 : calling is not a consteval function but it may become one. This
1465 : requires recursing; DECL may be promoted to consteval because it
1466 : contains an escalating expression E, but E itself may have to be
1467 : promoted first, etc. */
1468 155157192 : if (at_eof > 1 && unchecked_immediate_escalating_function_p (decl))
1469 : {
1470 : /* Set before the actual walk to avoid endless recursion. */
1471 5956488 : DECL_ESCALATION_CHECKED_P (decl) = true;
1472 : /* We're only looking for the first escalating expression. Let us not
1473 : walk more trees than necessary, hence mce_unknown. */
1474 5956488 : cp_fold_immediate (&DECL_SAVED_TREE (decl), mce_unknown, decl);
1475 : }
1476 :
1477 : /* [expr.const]p16 "An expression or conversion is immediate-escalating if
1478 : it is not initially in an immediate function context and it is either
1479 : -- an immediate invocation that is not a constant expression and is not
1480 : a subexpression of an immediate invocation."
1481 :
1482 : If we are in an immediate-escalating function, the immediate-escalating
1483 : expression or conversion makes it an immediate function. So STMT does
1484 : not need to produce a constant expression. */
1485 310314384 : if (DECL_IMMEDIATE_FUNCTION_P (decl))
1486 : {
1487 471249 : tree e = cxx_constant_value (stmt, tf_none);
1488 471249 : if (e == error_mark_node)
1489 : {
1490 : /* This takes care of, e.g.,
1491 : template <typename T>
1492 : constexpr int f(T t)
1493 : {
1494 : return id(t);
1495 : }
1496 : where id (consteval) causes f<int> to be promoted. */
1497 2916 : if (immediate_escalating_function_p (current_function_decl))
1498 2268 : promote_function_to_consteval (current_function_decl);
1499 648 : else if (complain & tf_error)
1500 : {
1501 536 : if (call_p)
1502 : {
1503 477 : auto_diagnostic_group d;
1504 477 : location_t loc = cp_expr_loc_or_input_loc (stmt);
1505 477 : error_at (loc, "call to consteval function %qE is "
1506 : "not a constant expression", stmt);
1507 : /* Explain why it's not a constant expression. */
1508 477 : cxx_constant_value (stmt, complain);
1509 477 : *stmt_p = error_mark_node;
1510 477 : maybe_explain_promoted_consteval (loc, decl);
1511 477 : }
1512 59 : else if (!data->pset.add (stmt))
1513 : {
1514 59 : taking_address_of_imm_fn_error (stmt, decl);
1515 59 : *stmt_p = build_zero_cst (TREE_TYPE (stmt));
1516 : }
1517 : /* If we're giving hard errors, continue the walk rather than
1518 : bailing out after the first error. */
1519 : return NULL_TREE;
1520 : }
1521 2380 : *walk_subtrees = 0;
1522 2380 : return stmt;
1523 : }
1524 :
1525 : /* We've evaluated the consteval function call. */
1526 468333 : if (call_p)
1527 : {
1528 564821 : if (code == CALL_EXPR && DECL_CONSTRUCTOR_P (decl))
1529 0 : *stmt_p = cp_build_init_expr_for_ctor (stmt, e);
1530 : else
1531 468333 : *stmt_p = e;
1532 : }
1533 : }
1534 : /* We've encountered a function call that may turn out to be consteval
1535 : later. Store its caller so that we can ensure that the call is
1536 : a constant expression. */
1537 154685943 : else if (unchecked_immediate_escalating_function_p (decl))
1538 : {
1539 : /* Make sure we're not inserting new elements while walking
1540 : the deferred_escalating_exprs hash table; if we are, it's
1541 : likely that a function wasn't properly marked checked for
1542 : i-e expressions. */
1543 26268539 : gcc_checking_assert (at_eof <= 1);
1544 26268539 : if (current_function_decl)
1545 26268009 : remember_escalating_expr (current_function_decl);
1546 : /* auto p = &f<int>; in the global scope won't be ensconced in
1547 : a function we could store for later at this point. (If there's
1548 : no c_f_d at this point and we're dealing with a call, we should
1549 : see the call when cp_fold_function __static_i_and_d.) */
1550 530 : else if (!call_p)
1551 143 : remember_escalating_expr (stmt);
1552 : }
1553 :
1554 : return NULL_TREE;
1555 : }
1556 :
1557 : /* A walk_tree helper to replace constant-initialized references in an
1558 : OMP_CLAUSE with the the declaration that they refer to. Such refs
1559 : will have been folded out in the body by cp_fold_non_odr_use_1 and
1560 : so we need to follow suit to prevent confusion. */
1561 :
1562 : static tree
1563 98217 : cp_fold_omp_clause_refs_r (tree *expr_p, int *walk_subtrees, void */*data*/)
1564 : {
1565 98217 : tree expr = *expr_p;
1566 :
1567 98217 : if (TYPE_P (expr))
1568 : {
1569 0 : *walk_subtrees = 0;
1570 0 : return NULL_TREE;
1571 : }
1572 :
1573 98217 : if (DECL_P (expr))
1574 : {
1575 53884 : *walk_subtrees = 0;
1576 :
1577 53884 : if (decl_constant_var_p (expr)
1578 53884 : && TYPE_REF_P (TREE_TYPE (expr)))
1579 : {
1580 548 : tree init = maybe_constant_value (expr);
1581 548 : if (TREE_CONSTANT (init))
1582 548 : *expr_p = tree_strip_nop_conversions (init);
1583 : }
1584 : }
1585 :
1586 : return NULL_TREE;
1587 : }
1588 :
1589 : /* Perform any pre-gimplification folding of C++ front end trees to
1590 : GENERIC.
1591 : Note: The folding of non-omp cases is something to move into
1592 : the middle-end. As for now we have most foldings only on GENERIC
1593 : in fold-const, we need to perform this before transformation to
1594 : GIMPLE-form.
1595 :
1596 : ??? This is algorithmically weird because walk_tree works in pre-order, so
1597 : we see outer expressions before inner expressions. This isn't as much of an
1598 : issue because cp_fold recurses into subexpressions in many cases, but then
1599 : walk_tree walks back into those subexpressions again. We avoid the
1600 : resulting complexity problem by caching the result of cp_fold, but it's
1601 : inelegant. */
1602 :
1603 : static tree
1604 4438669571 : cp_fold_r (tree *stmt_p, int *walk_subtrees, void *data_)
1605 : {
1606 4438669571 : cp_fold_data *data = (cp_fold_data*)data_;
1607 4438669571 : tree stmt = *stmt_p;
1608 4438669571 : enum tree_code code = TREE_CODE (stmt);
1609 :
1610 4438669571 : *stmt_p = stmt = cp_fold (*stmt_p, data->flags);
1611 :
1612 4438669571 : if (data->pset.add (stmt))
1613 : {
1614 : /* Don't walk subtrees of stmts we've already walked once, otherwise
1615 : we can have exponential complexity with e.g. lots of nested
1616 : SAVE_EXPRs or TARGET_EXPRs. cp_fold uses a cache and will return
1617 : always the same tree, which the first time cp_fold_r has been
1618 : called on it had the subtrees walked. */
1619 632451087 : *walk_subtrees = 0;
1620 632451087 : return NULL_TREE;
1621 : }
1622 :
1623 3806218484 : code = TREE_CODE (stmt);
1624 3806218484 : switch (code)
1625 : {
1626 52055 : tree x;
1627 52055 : int i, n;
1628 52055 : case OMP_FOR:
1629 52055 : case OMP_SIMD:
1630 52055 : case OMP_DISTRIBUTE:
1631 52055 : case OMP_LOOP:
1632 52055 : case OMP_TASKLOOP:
1633 52055 : case OMP_TILE:
1634 52055 : case OMP_UNROLL:
1635 52055 : case OACC_LOOP:
1636 52055 : cp_walk_tree (&OMP_FOR_BODY (stmt), cp_fold_r, data, NULL);
1637 52055 : cp_walk_tree (&OMP_FOR_CLAUSES (stmt), cp_fold_r, data, NULL);
1638 52055 : cp_walk_tree (&OMP_FOR_INIT (stmt), cp_fold_r, data, NULL);
1639 52055 : x = OMP_FOR_COND (stmt);
1640 52055 : if (x && TREE_CODE_CLASS (TREE_CODE (x)) == tcc_comparison)
1641 : {
1642 0 : cp_walk_tree (&TREE_OPERAND (x, 0), cp_fold_r, data, NULL);
1643 0 : cp_walk_tree (&TREE_OPERAND (x, 1), cp_fold_r, data, NULL);
1644 : }
1645 38403 : else if (x && TREE_CODE (x) == TREE_VEC)
1646 : {
1647 38403 : n = TREE_VEC_LENGTH (x);
1648 88148 : for (i = 0; i < n; i++)
1649 : {
1650 49745 : tree o = TREE_VEC_ELT (x, i);
1651 49745 : if (o && TREE_CODE_CLASS (TREE_CODE (o)) == tcc_comparison)
1652 47919 : cp_walk_tree (&TREE_OPERAND (o, 1), cp_fold_r, data, NULL);
1653 : }
1654 : }
1655 52055 : x = OMP_FOR_INCR (stmt);
1656 52055 : if (x && TREE_CODE (x) == TREE_VEC)
1657 : {
1658 38403 : n = TREE_VEC_LENGTH (x);
1659 88148 : for (i = 0; i < n; i++)
1660 : {
1661 49745 : tree o = TREE_VEC_ELT (x, i);
1662 49745 : if (o && TREE_CODE (o) == MODIFY_EXPR)
1663 12044 : o = TREE_OPERAND (o, 1);
1664 47919 : if (o && (TREE_CODE (o) == PLUS_EXPR || TREE_CODE (o) == MINUS_EXPR
1665 38775 : || TREE_CODE (o) == POINTER_PLUS_EXPR))
1666 : {
1667 12044 : cp_walk_tree (&TREE_OPERAND (o, 0), cp_fold_r, data, NULL);
1668 12044 : cp_walk_tree (&TREE_OPERAND (o, 1), cp_fold_r, data, NULL);
1669 : }
1670 : }
1671 : }
1672 52055 : cp_walk_tree (&OMP_FOR_PRE_BODY (stmt), cp_fold_r, data, NULL);
1673 52055 : *walk_subtrees = 0;
1674 52055 : return NULL_TREE;
1675 :
1676 167342 : case OMP_CLAUSE:
1677 167342 : if ((data->flags & ff_only_non_odr)
1678 83682 : && omp_clause_num_ops[OMP_CLAUSE_CODE (stmt)] >= 1
1679 70234 : && OMP_CLAUSE_CODE (stmt) >= OMP_CLAUSE_PRIVATE
1680 70234 : && OMP_CLAUSE_CODE (stmt) <= OMP_CLAUSE__SCANTEMP_
1681 214143 : && OMP_CLAUSE_DECL (stmt))
1682 : {
1683 45619 : tree *decl = &OMP_CLAUSE_DECL (stmt);
1684 45619 : cp_walk_tree (decl, cp_fold_omp_clause_refs_r, NULL, NULL);
1685 45619 : if (TREE_CODE (*decl) == ADDR_EXPR
1686 45619 : && DECL_P (TREE_OPERAND (*decl, 0)))
1687 164 : *decl = TREE_OPERAND (*decl, 0);
1688 45619 : data->pset.add (*decl);
1689 : }
1690 : break;
1691 :
1692 45968492 : case IF_STMT:
1693 45968492 : if (IF_STMT_CONSTEVAL_P (stmt))
1694 : {
1695 : /* Don't walk THEN_CLAUSE (stmt) for consteval if. IF_COND is always
1696 : boolean_false_node. */
1697 158680 : cp_walk_tree (&ELSE_CLAUSE (stmt), cp_fold_r, data, NULL);
1698 158680 : cp_walk_tree (&IF_SCOPE (stmt), cp_fold_r, data, NULL);
1699 158680 : *walk_subtrees = 0;
1700 158680 : return NULL_TREE;
1701 : }
1702 : break;
1703 :
1704 : /* cp_genericize_{init,target}_expr are only for genericize time; they're
1705 : here rather than in cp_genericize to avoid problems with the invisible
1706 : reference transition. */
1707 126656366 : case INIT_EXPR:
1708 126656366 : if (!flag_no_inline
1709 119541178 : && (data->flags & ff_genericize))
1710 : {
1711 59248175 : tree to = TREE_OPERAND (*stmt_p, 0);
1712 59248175 : tree &from = TREE_OPERAND (*stmt_p, 1);
1713 59248175 : tree folded = maybe_constant_init (from, to, data->mce ());
1714 59248175 : if (folded != from && TREE_CONSTANT (folded))
1715 1620771 : from = folded;
1716 : }
1717 :
1718 126656366 : if (data->flags & ff_genericize)
1719 62767749 : cp_genericize_init_expr (stmt_p);
1720 : break;
1721 :
1722 44206632 : case TARGET_EXPR:
1723 44206632 : if (!flag_no_inline
1724 41572073 : && (data->flags & ff_genericize))
1725 19415438 : if (tree &init = TARGET_EXPR_INITIAL (stmt))
1726 : {
1727 19415438 : tree folded = maybe_constant_init (init, TARGET_EXPR_SLOT (stmt),
1728 : data->mce ());
1729 19415438 : if (folded != init && TREE_CONSTANT (folded))
1730 1209334 : init = folded;
1731 : }
1732 :
1733 : /* This needs to happen between the constexpr evaluation (which wants
1734 : pre-generic trees) and fold (which wants the cp_genericize_init
1735 : transformations). */
1736 44206632 : if (data->flags & ff_genericize)
1737 20704603 : cp_genericize_target_expr (stmt_p);
1738 :
1739 44206632 : if (tree &init = TARGET_EXPR_INITIAL (stmt))
1740 : {
1741 44206632 : cp_walk_tree (&init, cp_fold_r, data, NULL);
1742 44206632 : cp_walk_tree (&TARGET_EXPR_CLEANUP (stmt), cp_fold_r, data, NULL);
1743 44206632 : *walk_subtrees = 0;
1744 : /* Folding might replace e.g. a COND_EXPR with a TARGET_EXPR; in
1745 : that case, strip it in favor of this one. */
1746 44206632 : if (TREE_CODE (init) == TARGET_EXPR)
1747 : {
1748 130 : tree sub = TARGET_EXPR_INITIAL (init);
1749 130 : maybe_replace_decl (&sub, TARGET_EXPR_SLOT (init),
1750 130 : TARGET_EXPR_SLOT (stmt));
1751 130 : init = sub;
1752 : }
1753 : }
1754 : break;
1755 :
1756 129867540 : case CONVERT_EXPR:
1757 : /* convert_to_void used to fold these away to void_node. Do it now;
1758 : other code (e.g., trees_out) depends on these being expunged. We
1759 : do it here before maybe_save_constexpr_fundef copies function
1760 : bodies. */
1761 129867540 : if ((data->flags & (ff_only_non_odr | ff_genericize))
1762 129741414 : && VOID_TYPE_P (TREE_TYPE (stmt))
1763 220868803 : && !TREE_SIDE_EFFECTS (stmt))
1764 : {
1765 : /* Since we're discarding it, it's our last chance to check for
1766 : out-of-consteval expressions. */
1767 384917 : check_out_of_consteval_use (stmt);
1768 384917 : *stmt_p = void_node;
1769 384917 : *walk_subtrees = 0;
1770 384917 : return NULL_TREE;
1771 : }
1772 : break;
1773 :
1774 : default:
1775 : break;
1776 : }
1777 :
1778 : return NULL_TREE;
1779 : }
1780 :
1781 : /* Fold ALL the trees! FIXME we should be able to remove this, but
1782 : apparently that still causes optimization regressions. */
1783 :
1784 : void
1785 67973379 : cp_fold_function (tree fndecl)
1786 : {
1787 : /* By now all manifestly-constant-evaluated expressions will have
1788 : been constant-evaluated already if possible, so we can safely
1789 : pass ff_mce_false. */
1790 67973379 : cp_fold_data data (ff_genericize | ff_mce_false);
1791 : /* Do cp_fold_immediate_r in separate whole IL walk instead of during
1792 : cp_fold_r, as otherwise expressions using results of immediate functions
1793 : might not be folded as cp_fold is called on those before cp_fold_r is
1794 : called on their argument. */
1795 67973379 : if (cxx_dialect >= cxx20)
1796 : {
1797 66018846 : cp_walk_tree (&DECL_SAVED_TREE (fndecl), cp_fold_immediate_r,
1798 : &data, NULL);
1799 66018846 : data.pset.empty ();
1800 : }
1801 67973379 : cp_walk_tree (&DECL_SAVED_TREE (fndecl), cp_fold_r, &data, NULL);
1802 :
1803 : /* This is merely an optimization: if FNDECL has no i-e expressions,
1804 : we'll not save c_f_d, and we can safely say that FNDECL will not
1805 : be promoted to consteval. */
1806 67973379 : if (deferred_escalating_exprs
1807 67973379 : && !deferred_escalating_exprs->contains (current_function_decl))
1808 50698885 : DECL_ESCALATION_CHECKED_P (fndecl) = true;
1809 67973379 : }
1810 :
1811 : /* Fold any non-ODR usages of constant variables in FNDECL. This occurs
1812 : before saving the constexpr fundef, so do as little other folding
1813 : as possible. */
1814 :
1815 : void
1816 69018106 : cp_fold_function_non_odr_use (tree fndecl)
1817 : {
1818 69018106 : cp_fold_data data (ff_only_non_odr);
1819 69018106 : cp_walk_tree (&DECL_SAVED_TREE (fndecl), cp_fold_r, &data, NULL);
1820 69018106 : }
1821 :
1822 : /* We've stashed immediate-escalating functions. Now see if they indeed
1823 : ought to be promoted to consteval. */
1824 :
1825 : void
1826 99305 : process_and_check_pending_immediate_escalating_fns ()
1827 : {
1828 : /* This will be null for -fno-immediate-escalation. */
1829 99305 : if (!deferred_escalating_exprs)
1830 : return;
1831 :
1832 14019257 : for (auto e : *deferred_escalating_exprs)
1833 13999693 : if (TREE_CODE (e) == FUNCTION_DECL && !DECL_ESCALATION_CHECKED_P (e))
1834 9564270 : cp_fold_immediate (&DECL_SAVED_TREE (e), mce_false, e);
1835 :
1836 : /* We've escalated every function that could have been promoted to
1837 : consteval. Check that we are not taking the address of a consteval
1838 : function. */
1839 14019257 : for (auto e : *deferred_escalating_exprs)
1840 : {
1841 13999693 : if (TREE_CODE (e) == FUNCTION_DECL)
1842 13999550 : continue;
1843 143 : tree decl = (TREE_CODE (e) == PTRMEM_CST
1844 143 : ? PTRMEM_CST_MEMBER (e)
1845 143 : : TREE_OPERAND (e, 0));
1846 286 : if (DECL_IMMEDIATE_FUNCTION_P (decl))
1847 12 : taking_address_of_imm_fn_error (e, decl);
1848 : }
1849 :
1850 19564 : deferred_escalating_exprs = nullptr;
1851 : }
1852 :
1853 : /* Turn SPACESHIP_EXPR EXPR into GENERIC. */
1854 :
1855 235400 : static tree genericize_spaceship (tree expr)
1856 : {
1857 235400 : iloc_sentinel s (cp_expr_location (expr));
1858 235400 : tree type = TREE_TYPE (expr);
1859 235400 : tree op0 = TREE_OPERAND (expr, 0);
1860 235400 : tree op1 = TREE_OPERAND (expr, 1);
1861 235400 : return genericize_spaceship (input_location, type, op0, op1);
1862 235400 : }
1863 :
1864 : /* If EXPR involves an anonymous VLA type, prepend a DECL_EXPR for that type
1865 : to trigger gimplify_type_sizes; otherwise a cast to pointer-to-VLA confuses
1866 : the middle-end (c++/88256). If EXPR is a DECL, use add_stmt and return
1867 : NULL_TREE; otherwise return a COMPOUND_STMT of the DECL_EXPR and EXPR. */
1868 :
1869 : tree
1870 142698074 : predeclare_vla (tree expr)
1871 : {
1872 142698074 : tree type = TREE_TYPE (expr);
1873 142698074 : if (type == error_mark_node)
1874 : return expr;
1875 142697936 : if (is_typedef_decl (expr))
1876 142697936 : type = DECL_ORIGINAL_TYPE (expr);
1877 :
1878 : /* We need to strip pointers for gimplify_type_sizes. */
1879 142697936 : tree vla = type;
1880 217550571 : while (POINTER_TYPE_P (vla))
1881 : {
1882 77372138 : if (TYPE_NAME (vla))
1883 : return expr;
1884 74852635 : vla = TREE_TYPE (vla);
1885 : }
1886 72912571 : if (vla == type || TYPE_NAME (vla)
1887 140749361 : || !variably_modified_type_p (vla, NULL_TREE))
1888 : return expr;
1889 :
1890 216 : tree decl = build_decl (input_location, TYPE_DECL, NULL_TREE, vla);
1891 216 : DECL_ARTIFICIAL (decl) = 1;
1892 216 : TYPE_NAME (vla) = decl;
1893 216 : tree dexp = build_stmt (input_location, DECL_EXPR, decl);
1894 216 : if (DECL_P (expr))
1895 : {
1896 5 : add_stmt (dexp);
1897 5 : return NULL_TREE;
1898 : }
1899 : else
1900 : {
1901 211 : expr = build2 (COMPOUND_EXPR, type, dexp, expr);
1902 211 : return expr;
1903 : }
1904 : }
1905 :
1906 : /* Perform any pre-gimplification lowering of C++ front end trees to
1907 : GENERIC. */
1908 :
1909 : static tree
1910 1851326975 : cp_genericize_r (tree *stmt_p, int *walk_subtrees, void *data)
1911 : {
1912 1873664807 : tree stmt = *stmt_p;
1913 1873664807 : struct cp_genericize_data *wtd = (struct cp_genericize_data *) data;
1914 1873664807 : hash_set<tree> *p_set = wtd->p_set;
1915 :
1916 : /* If in an OpenMP context, note var uses. */
1917 1873664807 : if (UNLIKELY (wtd->omp_ctx != NULL)
1918 603055 : && (VAR_P (stmt)
1919 : || TREE_CODE (stmt) == PARM_DECL
1920 : || TREE_CODE (stmt) == RESULT_DECL)
1921 1873773297 : && omp_var_to_track (stmt))
1922 12691 : omp_cxx_notice_variable (wtd->omp_ctx, stmt);
1923 :
1924 : /* Don't dereference parms in a thunk, pass the references through. */
1925 75528409 : if ((TREE_CODE (stmt) == CALL_EXPR && call_from_lambda_thunk_p (stmt))
1926 1949124947 : || (TREE_CODE (stmt) == AGGR_INIT_EXPR && AGGR_INIT_FROM_THUNK_P (stmt)))
1927 : {
1928 68483 : *walk_subtrees = 0;
1929 68483 : return NULL;
1930 : }
1931 :
1932 : /* Dereference invisible reference parms. */
1933 1873596324 : if (wtd->handle_invisiref_parm_p && is_invisiref_parm (stmt))
1934 : {
1935 1304015 : *stmt_p = convert_from_reference (stmt);
1936 1304015 : p_set->add (*stmt_p);
1937 1304015 : *walk_subtrees = 0;
1938 1304015 : return NULL;
1939 : }
1940 :
1941 : /* Map block scope extern declarations to visible declarations with the
1942 : same name and type in outer scopes if any. */
1943 1872292309 : if (VAR_OR_FUNCTION_DECL_P (stmt) && DECL_LOCAL_DECL_P (stmt))
1944 21653 : if (tree alias = DECL_LOCAL_DECL_ALIAS (stmt))
1945 : {
1946 21653 : if (alias != error_mark_node)
1947 : {
1948 21650 : *stmt_p = alias;
1949 21650 : TREE_USED (alias) |= TREE_USED (stmt);
1950 : }
1951 21653 : *walk_subtrees = 0;
1952 21653 : return NULL;
1953 : }
1954 :
1955 1872270656 : if (TREE_CODE (stmt) == INTEGER_CST
1956 215138126 : && TYPE_REF_P (TREE_TYPE (stmt))
1957 168 : && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))
1958 1872270657 : && !wtd->no_sanitize_p)
1959 : {
1960 1 : ubsan_maybe_instrument_reference (stmt_p);
1961 1 : if (*stmt_p != stmt)
1962 : {
1963 1 : *walk_subtrees = 0;
1964 1 : return NULL_TREE;
1965 : }
1966 : }
1967 :
1968 : /* Other than invisiref parms, don't walk the same tree twice. */
1969 1872270655 : if (p_set->contains (stmt))
1970 : {
1971 372802990 : *walk_subtrees = 0;
1972 372802990 : return NULL_TREE;
1973 : }
1974 :
1975 1499467665 : if ((TREE_CODE (stmt) == VAR_DECL
1976 : || TREE_CODE (stmt) == PARM_DECL
1977 : || TREE_CODE (stmt) == RESULT_DECL)
1978 160323187 : && DECL_HAS_VALUE_EXPR_P (stmt))
1979 : {
1980 855825 : tree ve = DECL_VALUE_EXPR (stmt);
1981 855825 : cp_walk_tree (&ve, cp_genericize_r, data, NULL);
1982 855825 : SET_DECL_VALUE_EXPR (stmt, ve);
1983 : }
1984 :
1985 1499467665 : switch (TREE_CODE (stmt))
1986 : {
1987 121233444 : case ADDR_EXPR:
1988 121233444 : if (is_invisiref_parm (TREE_OPERAND (stmt, 0)))
1989 : {
1990 : /* If in an OpenMP context, note var uses. */
1991 563709 : if (UNLIKELY (wtd->omp_ctx != NULL)
1992 563709 : && omp_var_to_track (TREE_OPERAND (stmt, 0)))
1993 412 : omp_cxx_notice_variable (wtd->omp_ctx, TREE_OPERAND (stmt, 0));
1994 563709 : *stmt_p = fold_convert (TREE_TYPE (stmt), TREE_OPERAND (stmt, 0));
1995 563709 : *walk_subtrees = 0;
1996 : }
1997 121233444 : check_out_of_consteval_use (stmt);
1998 121233444 : break;
1999 :
2000 44920604 : case RETURN_EXPR:
2001 44920604 : if (TREE_OPERAND (stmt, 0))
2002 : {
2003 44222392 : if (error_operand_p (TREE_OPERAND (stmt, 0))
2004 44222392 : && warn_return_type)
2005 : /* Suppress -Wreturn-type for this function. */
2006 15 : suppress_warning (current_function_decl, OPT_Wreturn_type);
2007 :
2008 44222392 : if (is_invisiref_parm (TREE_OPERAND (stmt, 0)))
2009 : /* Don't dereference an invisiref RESULT_DECL inside a
2010 : RETURN_EXPR. */
2011 680 : *walk_subtrees = 0;
2012 44222392 : if (RETURN_EXPR_LOCAL_ADDR_P (stmt))
2013 : {
2014 : /* Don't return the address of a local variable. */
2015 175 : tree *p = &TREE_OPERAND (stmt, 0);
2016 350 : while (TREE_CODE (*p) == COMPOUND_EXPR)
2017 0 : p = &TREE_OPERAND (*p, 0);
2018 175 : if (TREE_CODE (*p) == INIT_EXPR)
2019 : {
2020 175 : tree op = TREE_OPERAND (*p, 1);
2021 175 : tree new_op = build2 (COMPOUND_EXPR, TREE_TYPE (op), op,
2022 175 : build_zero_cst (TREE_TYPE (op)));
2023 175 : TREE_OPERAND (*p, 1) = new_op;
2024 : }
2025 : }
2026 : }
2027 : break;
2028 :
2029 83569 : case OMP_CLAUSE:
2030 83569 : switch (OMP_CLAUSE_CODE (stmt))
2031 : {
2032 2590 : case OMP_CLAUSE_LASTPRIVATE:
2033 : /* Don't dereference an invisiref in OpenMP clauses. */
2034 2590 : if (is_invisiref_parm (OMP_CLAUSE_DECL (stmt)))
2035 : {
2036 53 : *walk_subtrees = 0;
2037 53 : if (OMP_CLAUSE_LASTPRIVATE_STMT (stmt))
2038 48 : cp_walk_tree (&OMP_CLAUSE_LASTPRIVATE_STMT (stmt),
2039 : cp_genericize_r, data, NULL);
2040 : }
2041 : break;
2042 2094 : case OMP_CLAUSE_PRIVATE:
2043 : /* Don't dereference an invisiref in OpenMP clauses. */
2044 2094 : if (is_invisiref_parm (OMP_CLAUSE_DECL (stmt)))
2045 8 : *walk_subtrees = 0;
2046 2086 : else if (wtd->omp_ctx != NULL)
2047 : {
2048 : /* Private clause doesn't cause any references to the
2049 : var in outer contexts, avoid calling
2050 : omp_cxx_notice_variable for it. */
2051 584 : struct cp_genericize_omp_taskreg *old = wtd->omp_ctx;
2052 584 : wtd->omp_ctx = NULL;
2053 584 : cp_walk_tree (&OMP_CLAUSE_DECL (stmt), cp_genericize_r,
2054 : data, NULL);
2055 584 : wtd->omp_ctx = old;
2056 584 : *walk_subtrees = 0;
2057 : }
2058 : break;
2059 6095 : case OMP_CLAUSE_SHARED:
2060 6095 : case OMP_CLAUSE_FIRSTPRIVATE:
2061 6095 : case OMP_CLAUSE_COPYIN:
2062 6095 : case OMP_CLAUSE_COPYPRIVATE:
2063 6095 : case OMP_CLAUSE_INCLUSIVE:
2064 6095 : case OMP_CLAUSE_EXCLUSIVE:
2065 : /* Don't dereference an invisiref in OpenMP clauses. */
2066 6095 : if (is_invisiref_parm (OMP_CLAUSE_DECL (stmt)))
2067 87 : *walk_subtrees = 0;
2068 : break;
2069 8626 : case OMP_CLAUSE_REDUCTION:
2070 8626 : case OMP_CLAUSE_IN_REDUCTION:
2071 8626 : case OMP_CLAUSE_TASK_REDUCTION:
2072 : /* Don't dereference an invisiref in reduction clause's
2073 : OMP_CLAUSE_DECL either. OMP_CLAUSE_REDUCTION_{INIT,MERGE}
2074 : still needs to be genericized. */
2075 8626 : if (is_invisiref_parm (OMP_CLAUSE_DECL (stmt)))
2076 : {
2077 38 : *walk_subtrees = 0;
2078 38 : if (OMP_CLAUSE_REDUCTION_INIT (stmt))
2079 38 : cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (stmt),
2080 : cp_genericize_r, data, NULL);
2081 38 : if (OMP_CLAUSE_REDUCTION_MERGE (stmt))
2082 38 : cp_walk_tree (&OMP_CLAUSE_REDUCTION_MERGE (stmt),
2083 : cp_genericize_r, data, NULL);
2084 : }
2085 : break;
2086 : default:
2087 : break;
2088 : }
2089 : break;
2090 :
2091 : /* Due to the way voidify_wrapper_expr is written, we don't get a chance
2092 : to lower this construct before scanning it, so we need to lower these
2093 : before doing anything else. */
2094 5824968 : case CLEANUP_STMT:
2095 5824968 : *stmt_p = build2_loc (EXPR_LOCATION (stmt),
2096 5824968 : CLEANUP_EH_ONLY (stmt) ? TRY_CATCH_EXPR
2097 : : TRY_FINALLY_EXPR,
2098 : void_type_node,
2099 5824968 : CLEANUP_BODY (stmt),
2100 5824968 : CLEANUP_EXPR (stmt));
2101 5824968 : break;
2102 :
2103 22336773 : case IF_STMT:
2104 22336773 : genericize_if_stmt (stmt_p);
2105 : /* *stmt_p has changed, tail recurse to handle it again. */
2106 22336773 : return cp_genericize_r (stmt_p, walk_subtrees, data);
2107 :
2108 : /* COND_EXPR might have incompatible types in branches if one or both
2109 : arms are bitfields. Fix it up now. */
2110 19817710 : case COND_EXPR:
2111 19817710 : {
2112 19817710 : tree type_left
2113 19817710 : = (TREE_OPERAND (stmt, 1)
2114 19817710 : ? is_bitfield_expr_with_lowered_type (TREE_OPERAND (stmt, 1))
2115 : : NULL_TREE);
2116 19817710 : tree type_right
2117 19817710 : = (TREE_OPERAND (stmt, 2)
2118 19817710 : ? is_bitfield_expr_with_lowered_type (TREE_OPERAND (stmt, 2))
2119 : : NULL_TREE);
2120 19817710 : if (type_left
2121 19817743 : && !useless_type_conversion_p (TREE_TYPE (stmt),
2122 33 : TREE_TYPE (TREE_OPERAND (stmt, 1))))
2123 : {
2124 30 : TREE_OPERAND (stmt, 1)
2125 30 : = fold_convert (type_left, TREE_OPERAND (stmt, 1));
2126 30 : gcc_assert (useless_type_conversion_p (TREE_TYPE (stmt),
2127 : type_left));
2128 : }
2129 19817710 : if (type_right
2130 19817727 : && !useless_type_conversion_p (TREE_TYPE (stmt),
2131 17 : TREE_TYPE (TREE_OPERAND (stmt, 2))))
2132 : {
2133 17 : TREE_OPERAND (stmt, 2)
2134 17 : = fold_convert (type_right, TREE_OPERAND (stmt, 2));
2135 17 : gcc_assert (useless_type_conversion_p (TREE_TYPE (stmt),
2136 : type_right));
2137 : }
2138 : }
2139 : break;
2140 :
2141 26091170 : case BIND_EXPR:
2142 26091170 : if (UNLIKELY (wtd->omp_ctx != NULL))
2143 : {
2144 27558 : tree decl;
2145 33909 : for (decl = BIND_EXPR_VARS (stmt); decl; decl = DECL_CHAIN (decl))
2146 6351 : if (VAR_P (decl)
2147 6303 : && !DECL_EXTERNAL (decl)
2148 12654 : && omp_var_to_track (decl))
2149 : {
2150 586 : splay_tree_node n
2151 586 : = splay_tree_lookup (wtd->omp_ctx->variables,
2152 : (splay_tree_key) decl);
2153 586 : if (n == NULL)
2154 586 : splay_tree_insert (wtd->omp_ctx->variables,
2155 : (splay_tree_key) decl,
2156 586 : TREE_STATIC (decl)
2157 : ? OMP_CLAUSE_DEFAULT_SHARED
2158 : : OMP_CLAUSE_DEFAULT_PRIVATE);
2159 : }
2160 : }
2161 26091170 : if (sanitize_flags_p (SANITIZE_NULL | SANITIZE_ALIGNMENT | SANITIZE_VPTR))
2162 : {
2163 : /* The point here is to not sanitize static initializers. */
2164 3558 : bool no_sanitize_p = wtd->no_sanitize_p;
2165 3558 : wtd->no_sanitize_p = true;
2166 3558 : for (tree decl = BIND_EXPR_VARS (stmt);
2167 7006 : decl;
2168 3448 : decl = DECL_CHAIN (decl))
2169 3448 : if (VAR_P (decl)
2170 3031 : && TREE_STATIC (decl)
2171 3522 : && DECL_INITIAL (decl))
2172 12 : cp_walk_tree (&DECL_INITIAL (decl), cp_genericize_r, data, NULL);
2173 3558 : wtd->no_sanitize_p = no_sanitize_p;
2174 : }
2175 26091170 : if (flag_reflection)
2176 : /* Adjust consteval-only vars in BIND_EXPR_VARS so that REFLECT_EXPR
2177 : doesn't leak into the ME. */
2178 1174791 : for (tree *p = &BIND_EXPR_VARS (stmt); *p; )
2179 : {
2180 565174 : if (VAR_P (*p))
2181 : {
2182 : /* First, adjust null reflections. */
2183 483072 : if (DECL_INITIAL (*p))
2184 231459 : rewrite_null_reflection (DECL_INITIAL (*p));
2185 : /* If the variable is still consteval-only, remove it. */
2186 483072 : if (consteval_only_p (*p))
2187 : {
2188 884 : if (BIND_EXPR_BLOCK (stmt)
2189 884 : && *p == BLOCK_VARS (BIND_EXPR_BLOCK (stmt)))
2190 410 : BLOCK_VARS (BIND_EXPR_BLOCK (stmt)) = DECL_CHAIN (*p);
2191 884 : *p = DECL_CHAIN (*p);
2192 884 : continue;
2193 : }
2194 : }
2195 564290 : p = &DECL_CHAIN (*p);
2196 : }
2197 26091170 : wtd->bind_expr_stack.safe_push (stmt);
2198 26091170 : cp_walk_tree (&BIND_EXPR_BODY (stmt),
2199 : cp_genericize_r, data, NULL);
2200 26091170 : wtd->bind_expr_stack.pop ();
2201 26091170 : break;
2202 :
2203 1059 : case ASSERTION_STMT:
2204 1059 : case PRECONDITION_STMT:
2205 1059 : case POSTCONDITION_STMT:
2206 1059 : if (tree check = build_contract_check (stmt))
2207 : {
2208 1059 : *stmt_p = check;
2209 1059 : return cp_genericize_r (stmt_p, walk_subtrees, data);
2210 : }
2211 : /* If we didn't build a check, replace it with void_node so we don't
2212 : leak contracts into GENERIC. */
2213 0 : *stmt_p = void_node;
2214 0 : *walk_subtrees = 0;
2215 0 : break;
2216 :
2217 18455 : case USING_STMT:
2218 18455 : {
2219 18455 : tree block = NULL_TREE;
2220 :
2221 : /* Get the innermost inclosing GIMPLE_BIND that has a non NULL
2222 : BLOCK, and append an IMPORTED_DECL to its
2223 : BLOCK_VARS chained list. */
2224 18455 : if (wtd->bind_expr_stack.exists ())
2225 : {
2226 18455 : int i;
2227 18455 : for (i = wtd->bind_expr_stack.length () - 1; i >= 0; i--)
2228 18455 : if ((block = BIND_EXPR_BLOCK (wtd->bind_expr_stack[i])))
2229 : break;
2230 : }
2231 18455 : if (block)
2232 : {
2233 18455 : tree decl = TREE_OPERAND (stmt, 0);
2234 18455 : gcc_assert (decl);
2235 :
2236 18455 : if (undeduced_auto_decl (decl))
2237 : /* Omit from the GENERIC, the back-end can't handle it. */;
2238 : else
2239 : {
2240 18452 : tree using_directive = make_node (IMPORTED_DECL);
2241 18452 : TREE_TYPE (using_directive) = void_type_node;
2242 18452 : DECL_CONTEXT (using_directive) = current_function_decl;
2243 36904 : DECL_SOURCE_LOCATION (using_directive)
2244 18452 : = cp_expr_loc_or_input_loc (stmt);
2245 :
2246 18452 : IMPORTED_DECL_ASSOCIATED_DECL (using_directive) = decl;
2247 18452 : DECL_CHAIN (using_directive) = BLOCK_VARS (block);
2248 18452 : BLOCK_VARS (block) = using_directive;
2249 : }
2250 : }
2251 : /* The USING_STMT won't appear in GENERIC. */
2252 18455 : *stmt_p = build1 (NOP_EXPR, void_type_node, integer_zero_node);
2253 18455 : *walk_subtrees = 0;
2254 : }
2255 18455 : break;
2256 :
2257 25821051 : case DECL_EXPR:
2258 25821051 : if (TREE_CODE (DECL_EXPR_DECL (stmt)) == USING_DECL)
2259 : {
2260 : /* Using decls inside DECL_EXPRs are just dropped on the floor. */
2261 20760 : *stmt_p = build1 (NOP_EXPR, void_type_node, integer_zero_node);
2262 20760 : *walk_subtrees = 0;
2263 : }
2264 : else
2265 : {
2266 25800291 : tree d = DECL_EXPR_DECL (stmt);
2267 25800291 : if (VAR_P (d))
2268 51599023 : gcc_assert (CP_DECL_THREAD_LOCAL_P (d) == DECL_THREAD_LOCAL_P (d));
2269 : }
2270 : break;
2271 :
2272 11761 : case OMP_PARALLEL:
2273 11761 : case OMP_TASK:
2274 11761 : case OMP_TASKLOOP:
2275 11761 : {
2276 11761 : struct cp_genericize_omp_taskreg omp_ctx;
2277 11761 : tree c, decl;
2278 11761 : splay_tree_node n;
2279 :
2280 11761 : *walk_subtrees = 0;
2281 11761 : cp_walk_tree (&OMP_CLAUSES (stmt), cp_genericize_r, data, NULL);
2282 11761 : omp_ctx.is_parallel = TREE_CODE (stmt) == OMP_PARALLEL;
2283 11761 : omp_ctx.default_shared = omp_ctx.is_parallel;
2284 11761 : omp_ctx.outer = wtd->omp_ctx;
2285 11761 : omp_ctx.variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
2286 11761 : wtd->omp_ctx = &omp_ctx;
2287 27892 : for (c = OMP_CLAUSES (stmt); c; c = OMP_CLAUSE_CHAIN (c))
2288 16131 : switch (OMP_CLAUSE_CODE (c))
2289 : {
2290 4808 : case OMP_CLAUSE_SHARED:
2291 4808 : case OMP_CLAUSE_PRIVATE:
2292 4808 : case OMP_CLAUSE_FIRSTPRIVATE:
2293 4808 : case OMP_CLAUSE_LASTPRIVATE:
2294 4808 : decl = OMP_CLAUSE_DECL (c);
2295 4808 : if (decl == error_mark_node || !omp_var_to_track (decl))
2296 : break;
2297 520 : n = splay_tree_lookup (omp_ctx.variables, (splay_tree_key) decl);
2298 520 : if (n != NULL)
2299 : break;
2300 1022 : splay_tree_insert (omp_ctx.variables, (splay_tree_key) decl,
2301 511 : OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
2302 : ? OMP_CLAUSE_DEFAULT_SHARED
2303 : : OMP_CLAUSE_DEFAULT_PRIVATE);
2304 511 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_PRIVATE && omp_ctx.outer)
2305 91 : omp_cxx_notice_variable (omp_ctx.outer, decl);
2306 : break;
2307 1651 : case OMP_CLAUSE_DEFAULT:
2308 1651 : if (OMP_CLAUSE_DEFAULT_KIND (c) == OMP_CLAUSE_DEFAULT_SHARED)
2309 731 : omp_ctx.default_shared = true;
2310 : default:
2311 : break;
2312 : }
2313 11761 : if (TREE_CODE (stmt) == OMP_TASKLOOP)
2314 1001 : c_genericize_control_stmt (stmt_p, walk_subtrees, data,
2315 : cp_genericize_r, cp_walk_subtrees);
2316 : else
2317 10760 : cp_walk_tree (&OMP_BODY (stmt), cp_genericize_r, data, NULL);
2318 11761 : wtd->omp_ctx = omp_ctx.outer;
2319 11761 : splay_tree_delete (omp_ctx.variables);
2320 : }
2321 11761 : break;
2322 :
2323 6975 : case OMP_TARGET:
2324 6975 : cfun->has_omp_target = true;
2325 6975 : break;
2326 :
2327 141477 : case TRY_BLOCK:
2328 141477 : {
2329 141477 : *walk_subtrees = 0;
2330 141477 : tree try_block = wtd->try_block;
2331 141477 : wtd->try_block = stmt;
2332 141477 : cp_walk_tree (&TRY_STMTS (stmt), cp_genericize_r, data, NULL);
2333 141477 : wtd->try_block = try_block;
2334 141477 : cp_walk_tree (&TRY_HANDLERS (stmt), cp_genericize_r, data, NULL);
2335 : }
2336 141477 : break;
2337 :
2338 25463417 : case MUST_NOT_THROW_EXPR:
2339 : /* MUST_NOT_THROW_COND might be something else with TM. */
2340 25463417 : if (MUST_NOT_THROW_COND (stmt) == NULL_TREE)
2341 : {
2342 25463399 : *walk_subtrees = 0;
2343 25463399 : tree try_block = wtd->try_block;
2344 25463399 : wtd->try_block = stmt;
2345 25463399 : cp_walk_tree (&TREE_OPERAND (stmt, 0), cp_genericize_r, data, NULL);
2346 25463399 : wtd->try_block = try_block;
2347 : }
2348 : break;
2349 :
2350 147455 : case THROW_EXPR:
2351 147455 : {
2352 147455 : location_t loc = location_of (stmt);
2353 147455 : if (warning_suppressed_p (stmt /* What warning? */))
2354 : /* Never mind. */;
2355 47974 : else if (wtd->try_block)
2356 : {
2357 11155 : if (TREE_CODE (wtd->try_block) == MUST_NOT_THROW_EXPR)
2358 : {
2359 18 : auto_diagnostic_group d;
2360 31 : if (warning_at (loc, OPT_Wterminate,
2361 : "%<throw%> will always call %<terminate%>")
2362 10 : && cxx_dialect >= cxx11
2363 36 : && DECL_DESTRUCTOR_P (current_function_decl))
2364 5 : inform (loc, "in C++11 destructors default to %<noexcept%>");
2365 18 : }
2366 : }
2367 : else
2368 : {
2369 103 : if (warn_cxx11_compat && cxx_dialect < cxx11
2370 206 : && DECL_DESTRUCTOR_P (current_function_decl)
2371 1 : && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl))
2372 : == NULL_TREE)
2373 36820 : && (get_defaulted_eh_spec (current_function_decl)
2374 1 : == empty_except_spec))
2375 1 : warning_at (loc, OPT_Wc__11_compat,
2376 : "in C++11 this %<throw%> will call %<terminate%> "
2377 : "because destructors default to %<noexcept%>");
2378 : }
2379 : }
2380 : break;
2381 :
2382 42249480 : case CONVERT_EXPR:
2383 42249480 : gcc_checking_assert (!AGGREGATE_TYPE_P (TREE_TYPE (stmt)));
2384 42249480 : gcc_assert (!CONVERT_EXPR_VBASE_PATH (stmt));
2385 : break;
2386 :
2387 235400 : case SPACESHIP_EXPR:
2388 235400 : *stmt_p = genericize_spaceship (*stmt_p);
2389 235400 : break;
2390 :
2391 31802 : case PTRMEM_CST:
2392 : /* By the time we get here we're handing off to the back end, so we don't
2393 : need or want to preserve PTRMEM_CST anymore. */
2394 31802 : *stmt_p = cplus_expand_constant (stmt);
2395 31802 : *walk_subtrees = 0;
2396 31802 : break;
2397 :
2398 278570 : case MEM_REF:
2399 : /* For MEM_REF, make sure not to sanitize the second operand even
2400 : if it has reference type. It is just an offset with a type
2401 : holding other information. There is no other processing we
2402 : need to do for INTEGER_CSTs, so just ignore the second argument
2403 : unconditionally. */
2404 278570 : cp_walk_tree (&TREE_OPERAND (stmt, 0), cp_genericize_r, data, NULL);
2405 278570 : *walk_subtrees = 0;
2406 278570 : break;
2407 :
2408 41320650 : case INDIRECT_REF:
2409 : /* A pointer to a class might point to a base subobject, which is
2410 : only guaranteed the alignment of the class as a base (the ABI
2411 : "nvalign"). That is less than TYPE_ALIGN if a virtual base needs
2412 : more alignment than the rest of the class, so don't let the middle
2413 : end assume more (PR c++/103303). */
2414 41320650 : {
2415 41320650 : tree type = TREE_TYPE (stmt);
2416 30048670 : if (CLASS_TYPE_P (type)
2417 29855991 : && COMPLETE_TYPE_P (type)
2418 29295830 : && CLASSTYPE_AS_BASE (type)
2419 29295830 : && CLASSTYPE_ALIGN (type) < TYPE_ALIGN (type)
2420 95 : && resolves_to_fixed_type_p (stmt) <= 0
2421 : /* Respect an alignment the user specified on a variant. */
2422 41320739 : && TYPE_ALIGN (type) == TYPE_ALIGN (TYPE_MAIN_VARIANT (type)))
2423 89 : TREE_TYPE (stmt) = build_aligned_type (type, CLASSTYPE_ALIGN (type));
2424 : }
2425 : break;
2426 :
2427 119177167 : case NOP_EXPR:
2428 119177167 : *stmt_p = predeclare_vla (*stmt_p);
2429 :
2430 : /* Warn of new allocations that are not big enough for the target
2431 : type. Don't warn for calls from new expressions, new TYPE[0]
2432 : is fine and new expansion should arrange for the right size
2433 : to be provided. */
2434 119177167 : if (warn_alloc_size
2435 1086154 : && TREE_CODE (TREE_OPERAND (stmt, 0)) == CALL_EXPR
2436 64812 : && !CALL_FROM_NEW_OR_DELETE_P (TREE_OPERAND (stmt, 0))
2437 119241428 : && POINTER_TYPE_P (TREE_TYPE (stmt)))
2438 : {
2439 23096 : if (tree fndecl = get_callee_fndecl (TREE_OPERAND (stmt, 0)))
2440 23082 : if (DECL_IS_MALLOC (fndecl))
2441 : {
2442 629 : tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (fndecl));
2443 629 : tree alloc_size = lookup_attribute ("alloc_size", attrs);
2444 629 : if (alloc_size)
2445 627 : warn_for_alloc_size (EXPR_LOCATION (stmt),
2446 627 : TREE_TYPE (TREE_TYPE (stmt)),
2447 627 : TREE_OPERAND (stmt, 0), alloc_size);
2448 : }
2449 : }
2450 :
2451 119177167 : if (!wtd->no_sanitize_p
2452 119177162 : && sanitize_flags_p (SANITIZE_NULL | SANITIZE_ALIGNMENT)
2453 119193167 : && TYPE_REF_P (TREE_TYPE (stmt)))
2454 2506 : ubsan_maybe_instrument_reference (stmt_p);
2455 : break;
2456 :
2457 75455764 : case CALL_EXPR:
2458 75455764 : if (!wtd->no_sanitize_p
2459 75455764 : && sanitize_flags_p ((SANITIZE_NULL
2460 : | SANITIZE_ALIGNMENT | SANITIZE_VPTR)))
2461 : {
2462 16146 : tree fn = CALL_EXPR_FN (stmt);
2463 16146 : if (fn != NULL_TREE
2464 10015 : && !error_operand_p (fn)
2465 10015 : && INDIRECT_TYPE_P (TREE_TYPE (fn))
2466 26161 : && TREE_CODE (TREE_TYPE (TREE_TYPE (fn))) == METHOD_TYPE)
2467 : {
2468 5675 : bool is_ctor
2469 5675 : = TREE_CODE (fn) == ADDR_EXPR
2470 5552 : && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
2471 16779 : && DECL_CONSTRUCTOR_P (TREE_OPERAND (fn, 0));
2472 5675 : if (sanitize_flags_p (SANITIZE_NULL | SANITIZE_ALIGNMENT))
2473 5064 : ubsan_maybe_instrument_member_call (stmt, is_ctor);
2474 5675 : if (sanitize_flags_p (SANITIZE_VPTR) && !is_ctor)
2475 4830 : cp_ubsan_maybe_instrument_member_call (stmt);
2476 : }
2477 10471 : else if (fn == NULL_TREE
2478 6131 : && CALL_EXPR_IFN (stmt) == IFN_UBSAN_NULL
2479 5024 : && TREE_CODE (CALL_EXPR_ARG (stmt, 0)) == INTEGER_CST
2480 10477 : && TYPE_REF_P (TREE_TYPE (CALL_EXPR_ARG (stmt, 0))))
2481 6 : *walk_subtrees = 0;
2482 : }
2483 : /* Fall through. */
2484 80284993 : case AGGR_INIT_EXPR:
2485 : /* For calls to a multi-versioned function, overload resolution
2486 : returns the function with the highest target priority, that is,
2487 : the version that will checked for dispatching first. If this
2488 : version is inlinable, a direct call to this version can be made
2489 : otherwise the call should go through the dispatcher.
2490 : This is done at multiple_target.cc for target_version semantics. */
2491 80284993 : {
2492 80284993 : tree fn = cp_get_callee_fndecl_nofold (stmt);
2493 80284993 : if (TARGET_HAS_FMV_TARGET_ATTRIBUTE
2494 : && fn
2495 79123874 : && DECL_FUNCTION_VERSIONED (fn)
2496 80285128 : && (current_function_decl == NULL
2497 135 : || !targetm.target_option.can_inline_p
2498 135 : (current_function_decl, fn)))
2499 123 : if (tree dis = get_function_version_dispatcher (fn))
2500 : {
2501 123 : mark_versions_used (dis);
2502 123 : dis = build_address (dis);
2503 123 : if (TREE_CODE (stmt) == CALL_EXPR)
2504 120 : CALL_EXPR_FN (stmt) = dis;
2505 : else
2506 3 : AGGR_INIT_EXPR_FN (stmt) = dis;
2507 : }
2508 : }
2509 : break;
2510 :
2511 19470005 : case TARGET_EXPR:
2512 19470005 : if (TARGET_EXPR_INITIAL (stmt)
2513 19470005 : && TREE_CODE (TARGET_EXPR_INITIAL (stmt)) == CONSTRUCTOR
2514 21901084 : && CONSTRUCTOR_PLACEHOLDER_BOUNDARY (TARGET_EXPR_INITIAL (stmt)))
2515 169 : TARGET_EXPR_NO_ELIDE (stmt) = 1;
2516 : break;
2517 :
2518 642 : case TEMPLATE_ID_EXPR:
2519 642 : gcc_assert (concept_check_p (stmt));
2520 : /* Emit the value of the concept check. */
2521 642 : *stmt_p = evaluate_concept_check (stmt);
2522 642 : walk_subtrees = 0;
2523 642 : break;
2524 :
2525 4241 : case OMP_DISTRIBUTE:
2526 : /* Need to explicitly instantiate copy ctors on class iterators of
2527 : composite distribute parallel for. */
2528 4241 : if (OMP_FOR_INIT (*stmt_p) == NULL_TREE)
2529 : {
2530 3742 : tree *data[4] = { NULL, NULL, NULL, NULL };
2531 3742 : tree inner = walk_tree (&OMP_FOR_BODY (*stmt_p),
2532 : find_combined_omp_for, data, NULL);
2533 3742 : if (inner != NULL_TREE
2534 3708 : && TREE_CODE (inner) == OMP_FOR)
2535 : {
2536 4554 : for (int i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (inner)); i++)
2537 2859 : if (TREE_VEC_ELT (OMP_FOR_INIT (inner), i)
2538 2843 : && OMP_FOR_ORIG_DECLS (inner)
2539 2843 : && TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner),
2540 : i)) == TREE_LIST
2541 2883 : && TREE_PURPOSE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner),
2542 : i)))
2543 : {
2544 12 : tree orig = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner), i);
2545 : /* Class iterators aren't allowed on OMP_SIMD, so the only
2546 : case we need to solve is distribute parallel for. */
2547 12 : gcc_assert (TREE_CODE (inner) == OMP_FOR
2548 : && data[1]);
2549 12 : tree orig_decl = TREE_PURPOSE (orig);
2550 12 : tree c, cl = NULL_TREE;
2551 12 : for (c = OMP_FOR_CLAUSES (inner);
2552 16 : c; c = OMP_CLAUSE_CHAIN (c))
2553 12 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
2554 5 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
2555 13 : && OMP_CLAUSE_DECL (c) == orig_decl)
2556 : {
2557 : cl = c;
2558 : break;
2559 : }
2560 12 : if (cl == NULL_TREE)
2561 : {
2562 4 : for (c = OMP_PARALLEL_CLAUSES (*data[1]);
2563 4 : c; c = OMP_CLAUSE_CHAIN (c))
2564 1 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
2565 1 : && OMP_CLAUSE_DECL (c) == orig_decl)
2566 : {
2567 : cl = c;
2568 : break;
2569 : }
2570 : }
2571 4 : if (cl)
2572 : {
2573 9 : orig_decl = require_complete_type (orig_decl);
2574 9 : tree inner_type = TREE_TYPE (orig_decl);
2575 9 : if (orig_decl == error_mark_node)
2576 0 : continue;
2577 9 : if (TYPE_REF_P (TREE_TYPE (orig_decl)))
2578 0 : inner_type = TREE_TYPE (inner_type);
2579 :
2580 9 : while (TREE_CODE (inner_type) == ARRAY_TYPE)
2581 0 : inner_type = TREE_TYPE (inner_type);
2582 9 : get_copy_ctor (inner_type, tf_warning_or_error);
2583 : }
2584 : }
2585 : }
2586 : }
2587 : /* FALLTHRU */
2588 :
2589 5035904 : case FOR_STMT:
2590 5035904 : case WHILE_STMT:
2591 5035904 : case DO_STMT:
2592 5035904 : case SWITCH_STMT:
2593 5035904 : case CONTINUE_STMT:
2594 5035904 : case BREAK_STMT:
2595 5035904 : case OMP_FOR:
2596 5035904 : case OMP_SIMD:
2597 5035904 : case OMP_LOOP:
2598 5035904 : case OMP_TILE:
2599 5035904 : case OMP_UNROLL:
2600 5035904 : case OACC_LOOP:
2601 : /* These cases are handled by shared code. */
2602 5035904 : c_genericize_control_stmt (stmt_p, walk_subtrees, data,
2603 : cp_genericize_r, cp_walk_subtrees);
2604 5035904 : break;
2605 :
2606 69949300 : case STATEMENT_LIST:
2607 : /* As above, handled by shared code. */
2608 69949300 : c_genericize_control_stmt (stmt_p, walk_subtrees, data,
2609 : cp_genericize_r, cp_walk_subtrees);
2610 : /* If a statement list is freed as part of genericisation it will be
2611 : pushed onto the top of a statement list cache stack. A subsequent
2612 : action can cause a new statement list to be required - and the one
2613 : just pushed will be returned. If that is marked as visited, it can
2614 : prevent a tail recursion from processing the 'new' statement list,
2615 : so we do not mark statement lists as visited. */
2616 69949300 : return NULL_TREE;
2617 76405 : break;
2618 :
2619 76405 : case BIT_CAST_EXPR:
2620 76405 : *stmt_p = build1_loc (EXPR_LOCATION (stmt), VIEW_CONVERT_EXPR,
2621 76405 : TREE_TYPE (stmt), TREE_OPERAND (stmt, 0));
2622 76405 : break;
2623 :
2624 22012637 : case MODIFY_EXPR:
2625 : /* Mark stores to parts of complex automatic non-addressable
2626 : variables as DECL_NOT_GIMPLE_REG_P for -O0. This can't be
2627 : done during gimplification. See PR119120. */
2628 22012637 : if ((TREE_CODE (TREE_OPERAND (stmt, 0)) == REALPART_EXPR
2629 21984375 : || TREE_CODE (TREE_OPERAND (stmt, 0)) == IMAGPART_EXPR)
2630 56535 : && !optimize
2631 552 : && DECL_P (TREE_OPERAND (TREE_OPERAND (stmt, 0), 0))
2632 22012731 : && is_gimple_reg (TREE_OPERAND (TREE_OPERAND (stmt, 0), 0)))
2633 50 : DECL_NOT_GIMPLE_REG_P (TREE_OPERAND (TREE_OPERAND (stmt, 0), 0)) = 1;
2634 : break;
2635 :
2636 151 : case REFLECT_EXPR:
2637 : /* Only the null reflection may reach the ME. */
2638 151 : check_out_of_consteval_use (stmt);
2639 151 : *stmt_p = build_int_cst (meta_info_type_node, 0);
2640 151 : break;
2641 :
2642 807424671 : default:
2643 807424671 : if (IS_TYPE_OR_DECL_P (stmt))
2644 264238567 : *walk_subtrees = 0;
2645 : break;
2646 : }
2647 :
2648 1407180533 : p_set->add (*stmt_p);
2649 :
2650 1407180533 : return NULL;
2651 : }
2652 :
2653 : /* Lower C++ front end trees to GENERIC in T_P. */
2654 :
2655 : static void
2656 51479064 : cp_genericize_tree (tree* t_p, bool handle_invisiref_parm_p)
2657 : {
2658 51479064 : struct cp_genericize_data wtd;
2659 :
2660 51479064 : wtd.p_set = new hash_set<tree>;
2661 51479064 : wtd.bind_expr_stack.create (0);
2662 51479064 : wtd.omp_ctx = NULL;
2663 51479064 : wtd.try_block = NULL_TREE;
2664 51479064 : wtd.no_sanitize_p = false;
2665 51479064 : wtd.handle_invisiref_parm_p = handle_invisiref_parm_p;
2666 51479064 : cp_walk_tree (t_p, cp_genericize_r, &wtd, NULL);
2667 102958128 : delete wtd.p_set;
2668 51479064 : if (sanitize_flags_p (SANITIZE_VPTR))
2669 5766 : cp_ubsan_instrument_member_accesses (t_p);
2670 51479064 : }
2671 :
2672 : /* If a function that should end with a return in non-void
2673 : function doesn't obviously end with return, add ubsan
2674 : instrumentation code to verify it at runtime. If -fsanitize=return
2675 : is not enabled, instrument __builtin_unreachable. */
2676 :
2677 : static void
2678 51479064 : cp_maybe_instrument_return (tree fndecl)
2679 : {
2680 51479064 : if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fndecl)))
2681 73884286 : || DECL_CONSTRUCTOR_P (fndecl)
2682 36942143 : || DECL_DESTRUCTOR_P (fndecl)
2683 88421207 : || !targetm.warn_func_return (fndecl))
2684 : return;
2685 :
2686 36942131 : if (!sanitize_flags_p (SANITIZE_RETURN, fndecl)
2687 : /* Don't add __builtin_unreachable () if not optimizing, it will not
2688 : improve any optimizations in that case, just break UB code.
2689 : Don't add it if -fsanitize=unreachable -fno-sanitize=return either,
2690 : UBSan covers this with ubsan_instrument_return above where sufficient
2691 : information is provided, while the __builtin_unreachable () below
2692 : if return sanitization is disabled will just result in hard to
2693 : understand runtime error without location. */
2694 36942131 : && ((!optimize && !flag_unreachable_traps)
2695 36938237 : || sanitize_flags_p (SANITIZE_UNREACHABLE, fndecl)))
2696 : return;
2697 :
2698 36942101 : tree t = DECL_SAVED_TREE (fndecl);
2699 63869360 : while (t)
2700 : {
2701 63869360 : switch (TREE_CODE (t))
2702 : {
2703 4929869 : case BIND_EXPR:
2704 4929869 : t = BIND_EXPR_BODY (t);
2705 4929869 : continue;
2706 9079146 : case TRY_FINALLY_EXPR:
2707 9079146 : case CLEANUP_POINT_EXPR:
2708 9079146 : t = TREE_OPERAND (t, 0);
2709 9079146 : continue;
2710 12921051 : case STATEMENT_LIST:
2711 12921051 : {
2712 12921051 : tree_stmt_iterator i = tsi_last (t);
2713 12924730 : while (!tsi_end_p (i))
2714 : {
2715 12921923 : tree p = tsi_stmt (i);
2716 12921923 : if (TREE_CODE (p) != DEBUG_BEGIN_STMT)
2717 : break;
2718 3679 : tsi_prev (&i);
2719 : }
2720 12921051 : if (!tsi_end_p (i))
2721 : {
2722 12918244 : t = tsi_stmt (i);
2723 12918244 : continue;
2724 : }
2725 : }
2726 2807 : break;
2727 : case RETURN_EXPR:
2728 : return;
2729 : default:
2730 : break;
2731 14009015 : }
2732 : break;
2733 : }
2734 20297150 : if (t == NULL_TREE)
2735 : return;
2736 20297150 : tree *p = &DECL_SAVED_TREE (fndecl);
2737 20297150 : if (TREE_CODE (*p) == BIND_EXPR)
2738 927962 : p = &BIND_EXPR_BODY (*p);
2739 :
2740 20297150 : location_t loc = DECL_SOURCE_LOCATION (fndecl);
2741 20297150 : if (sanitize_flags_p (SANITIZE_RETURN, fndecl))
2742 1807 : t = ubsan_instrument_return (loc);
2743 : else
2744 20295343 : t = build_builtin_unreachable (BUILTINS_LOCATION);
2745 :
2746 20297150 : append_to_statement_list (t, p);
2747 : }
2748 :
2749 : void
2750 67972891 : cp_genericize (tree fndecl)
2751 : {
2752 67972891 : tree t;
2753 :
2754 : /* Fix up the types of parms passed by invisible reference. */
2755 179992002 : for (t = DECL_ARGUMENTS (fndecl); t; t = DECL_CHAIN (t))
2756 112019111 : if (TREE_ADDRESSABLE (TREE_TYPE (t)))
2757 : {
2758 : /* If a function's arguments are copied to create a thunk,
2759 : then DECL_BY_REFERENCE will be set -- but the type of the
2760 : argument will be a pointer type, so we will never get
2761 : here. */
2762 132521 : gcc_assert (!DECL_BY_REFERENCE (t));
2763 132521 : gcc_assert (DECL_ARG_TYPE (t) != TREE_TYPE (t));
2764 132521 : TREE_TYPE (t) = DECL_ARG_TYPE (t);
2765 132521 : DECL_BY_REFERENCE (t) = 1;
2766 132521 : TREE_ADDRESSABLE (t) = 0;
2767 132521 : relayout_decl (t);
2768 : }
2769 :
2770 : /* Do the same for the return value. */
2771 67972891 : if (TREE_ADDRESSABLE (TREE_TYPE (DECL_RESULT (fndecl))))
2772 : {
2773 1154402 : t = DECL_RESULT (fndecl);
2774 1154402 : TREE_TYPE (t) = build_reference_type (TREE_TYPE (t));
2775 1154402 : DECL_BY_REFERENCE (t) = 1;
2776 1154402 : TREE_ADDRESSABLE (t) = 0;
2777 1154402 : relayout_decl (t);
2778 1154402 : if (DECL_NAME (t))
2779 : {
2780 : /* Adjust DECL_VALUE_EXPR of the original var. */
2781 128467 : tree outer = outer_curly_brace_block (current_function_decl);
2782 128467 : tree var;
2783 :
2784 128467 : if (outer)
2785 300725 : for (var = BLOCK_VARS (outer); var; var = DECL_CHAIN (var))
2786 299997 : if (VAR_P (var)
2787 289904 : && DECL_NAME (t) == DECL_NAME (var)
2788 127739 : && DECL_HAS_VALUE_EXPR_P (var)
2789 427736 : && DECL_VALUE_EXPR (var) == t)
2790 : {
2791 127739 : tree val = convert_from_reference (t);
2792 127739 : SET_DECL_VALUE_EXPR (var, val);
2793 127739 : break;
2794 : }
2795 : }
2796 : }
2797 :
2798 : /* If we're a clone, the body is already GIMPLE. */
2799 67972891 : if (DECL_CLONED_FUNCTION_P (fndecl))
2800 16493827 : return;
2801 :
2802 : /* Allow cp_genericize calls to be nested. */
2803 51479064 : bc_state_t save_state;
2804 51479064 : save_bc_state (&save_state);
2805 :
2806 : /* We do want to see every occurrence of the parms, so we can't just use
2807 : walk_tree's hash functionality. */
2808 51479064 : cp_genericize_tree (&DECL_SAVED_TREE (fndecl), true);
2809 :
2810 51479064 : cp_maybe_instrument_return (fndecl);
2811 :
2812 : /* Do everything else. */
2813 51479064 : c_genericize (fndecl);
2814 51479064 : restore_bc_state (&save_state);
2815 : }
2816 :
2817 : /* Build code to apply FN to each member of ARG1 and ARG2. FN may be
2818 : NULL if there is in fact nothing to do. ARG2 may be null if FN
2819 : actually only takes one argument. */
2820 :
2821 : static tree
2822 3722 : cxx_omp_clause_apply_fn (tree fn, tree arg1, tree arg2)
2823 : {
2824 3722 : tree defparm, parm, t;
2825 3722 : int i = 0;
2826 3722 : int nargs;
2827 3722 : tree *argarray;
2828 :
2829 3722 : if (fn == NULL)
2830 : return NULL;
2831 :
2832 2848 : nargs = list_length (DECL_ARGUMENTS (fn));
2833 2848 : argarray = XALLOCAVEC (tree, nargs);
2834 :
2835 2848 : defparm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
2836 2848 : if (arg2)
2837 944 : defparm = TREE_CHAIN (defparm);
2838 :
2839 2848 : bool is_method = TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE;
2840 2848 : if (TREE_CODE (TREE_TYPE (arg1)) == ARRAY_TYPE)
2841 : {
2842 27 : tree inner_type = TREE_TYPE (arg1);
2843 27 : tree start1, end1, p1;
2844 27 : tree start2 = NULL, p2 = NULL;
2845 27 : tree ret = NULL, lab;
2846 :
2847 27 : start1 = arg1;
2848 27 : start2 = arg2;
2849 27 : do
2850 : {
2851 27 : inner_type = TREE_TYPE (inner_type);
2852 27 : start1 = build4 (ARRAY_REF, inner_type, start1,
2853 : size_zero_node, NULL, NULL);
2854 27 : if (arg2)
2855 9 : start2 = build4 (ARRAY_REF, inner_type, start2,
2856 : size_zero_node, NULL, NULL);
2857 : }
2858 27 : while (TREE_CODE (inner_type) == ARRAY_TYPE);
2859 27 : start1 = build_fold_addr_expr_loc (input_location, start1);
2860 27 : if (arg2)
2861 9 : start2 = build_fold_addr_expr_loc (input_location, start2);
2862 :
2863 27 : end1 = TYPE_SIZE_UNIT (TREE_TYPE (arg1));
2864 27 : end1 = fold_build_pointer_plus (start1, end1);
2865 :
2866 27 : p1 = create_tmp_var (TREE_TYPE (start1));
2867 27 : t = build2 (MODIFY_EXPR, TREE_TYPE (p1), p1, start1);
2868 27 : append_to_statement_list (t, &ret);
2869 :
2870 27 : if (arg2)
2871 : {
2872 9 : p2 = create_tmp_var (TREE_TYPE (start2));
2873 9 : t = build2 (MODIFY_EXPR, TREE_TYPE (p2), p2, start2);
2874 9 : append_to_statement_list (t, &ret);
2875 : }
2876 :
2877 27 : lab = create_artificial_label (input_location);
2878 27 : t = build1 (LABEL_EXPR, void_type_node, lab);
2879 27 : append_to_statement_list (t, &ret);
2880 :
2881 27 : argarray[i++] = p1;
2882 27 : if (arg2)
2883 9 : argarray[i++] = p2;
2884 : /* Handle default arguments. */
2885 27 : for (parm = defparm; parm && parm != void_list_node;
2886 0 : parm = TREE_CHAIN (parm), i++)
2887 0 : argarray[i] = convert_default_arg (TREE_VALUE (parm),
2888 0 : TREE_PURPOSE (parm), fn,
2889 : i - is_method, tf_warning_or_error);
2890 27 : t = build_call_a (fn, i, argarray);
2891 27 : if (MAYBE_CLASS_TYPE_P (TREE_TYPE (t)))
2892 0 : t = build_cplus_new (TREE_TYPE (t), t, tf_warning_or_error);
2893 27 : t = fold_convert (void_type_node, t);
2894 27 : t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
2895 27 : append_to_statement_list (t, &ret);
2896 :
2897 27 : t = fold_build_pointer_plus (p1, TYPE_SIZE_UNIT (inner_type));
2898 27 : t = build2 (MODIFY_EXPR, TREE_TYPE (p1), p1, t);
2899 27 : append_to_statement_list (t, &ret);
2900 :
2901 27 : if (arg2)
2902 : {
2903 9 : t = fold_build_pointer_plus (p2, TYPE_SIZE_UNIT (inner_type));
2904 9 : t = build2 (MODIFY_EXPR, TREE_TYPE (p2), p2, t);
2905 9 : append_to_statement_list (t, &ret);
2906 : }
2907 :
2908 27 : t = build2 (NE_EXPR, boolean_type_node, p1, end1);
2909 27 : t = build3 (COND_EXPR, void_type_node, t, build_and_jump (&lab), NULL);
2910 27 : append_to_statement_list (t, &ret);
2911 :
2912 27 : return ret;
2913 : }
2914 : else
2915 : {
2916 2821 : argarray[i++] = build_fold_addr_expr_loc (input_location, arg1);
2917 2821 : if (arg2)
2918 935 : argarray[i++] = build_fold_addr_expr_loc (input_location, arg2);
2919 : /* Handle default arguments. */
2920 2826 : for (parm = defparm; parm && parm != void_list_node;
2921 5 : parm = TREE_CHAIN (parm), i++)
2922 10 : argarray[i] = convert_default_arg (TREE_VALUE (parm),
2923 5 : TREE_PURPOSE (parm), fn,
2924 : i - is_method, tf_warning_or_error);
2925 2821 : t = build_call_a (fn, i, argarray);
2926 2821 : if (MAYBE_CLASS_TYPE_P (TREE_TYPE (t)))
2927 1 : t = build_cplus_new (TREE_TYPE (t), t, tf_warning_or_error);
2928 2821 : t = fold_convert (void_type_node, t);
2929 2821 : return fold_build_cleanup_point_expr (TREE_TYPE (t), t);
2930 : }
2931 : }
2932 :
2933 : /* Return code to initialize DECL with its default constructor, or
2934 : NULL if there's nothing to do. */
2935 :
2936 : tree
2937 42688 : cxx_omp_clause_default_ctor (tree clause, tree decl, tree /*outer*/)
2938 : {
2939 42688 : tree info = CP_OMP_CLAUSE_INFO (clause);
2940 42688 : tree ret = NULL;
2941 :
2942 42688 : if (info)
2943 1433 : ret = cxx_omp_clause_apply_fn (TREE_VEC_ELT (info, 0), decl, NULL);
2944 :
2945 42688 : return ret;
2946 : }
2947 :
2948 : /* Return code to initialize DST with a copy constructor from SRC. */
2949 :
2950 : tree
2951 12402 : cxx_omp_clause_copy_ctor (tree clause, tree dst, tree src)
2952 : {
2953 12402 : tree info = CP_OMP_CLAUSE_INFO (clause);
2954 12402 : tree ret = NULL;
2955 :
2956 12402 : if (info)
2957 284 : ret = cxx_omp_clause_apply_fn (TREE_VEC_ELT (info, 0), dst, src);
2958 284 : if (ret == NULL)
2959 12184 : ret = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
2960 :
2961 12402 : return ret;
2962 : }
2963 :
2964 : /* Similarly, except use an assignment operator instead. */
2965 :
2966 : tree
2967 12686 : cxx_omp_clause_assign_op (tree clause, tree dst, tree src)
2968 : {
2969 12686 : tree info = CP_OMP_CLAUSE_INFO (clause);
2970 12686 : tree ret = NULL;
2971 :
2972 12686 : if (info)
2973 748 : ret = cxx_omp_clause_apply_fn (TREE_VEC_ELT (info, 2), dst, src);
2974 748 : if (ret == NULL)
2975 11960 : ret = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
2976 :
2977 12686 : return ret;
2978 : }
2979 :
2980 : /* Return code to destroy DECL. */
2981 :
2982 : tree
2983 62733 : cxx_omp_clause_dtor (tree clause, tree decl)
2984 : {
2985 62733 : tree info = CP_OMP_CLAUSE_INFO (clause);
2986 62733 : tree ret = NULL;
2987 :
2988 62733 : if (info)
2989 1257 : ret = cxx_omp_clause_apply_fn (TREE_VEC_ELT (info, 1), decl, NULL);
2990 :
2991 62733 : return ret;
2992 : }
2993 :
2994 : /* True if OpenMP should privatize what this DECL points to rather
2995 : than the DECL itself. */
2996 :
2997 : bool
2998 982537 : cxx_omp_privatize_by_reference (const_tree decl)
2999 : {
3000 982537 : return (TYPE_REF_P (TREE_TYPE (decl))
3001 982537 : || is_invisiref_parm (decl));
3002 : }
3003 :
3004 : /* Return true if DECL is const qualified var having no mutable member. */
3005 : bool
3006 15468 : cxx_omp_const_qual_no_mutable (tree decl)
3007 : {
3008 15468 : tree type = TREE_TYPE (decl);
3009 15468 : if (TYPE_REF_P (type))
3010 : {
3011 896 : if (!is_invisiref_parm (decl))
3012 : return false;
3013 0 : type = TREE_TYPE (type);
3014 :
3015 0 : if (TREE_CODE (decl) == RESULT_DECL && DECL_NAME (decl))
3016 : {
3017 : /* NVR doesn't preserve const qualification of the
3018 : variable's type. */
3019 0 : tree outer = outer_curly_brace_block (current_function_decl);
3020 0 : tree var;
3021 :
3022 0 : if (outer)
3023 0 : for (var = BLOCK_VARS (outer); var; var = DECL_CHAIN (var))
3024 0 : if (VAR_P (var)
3025 0 : && DECL_NAME (decl) == DECL_NAME (var)
3026 0 : && (TYPE_MAIN_VARIANT (type)
3027 0 : == TYPE_MAIN_VARIANT (TREE_TYPE (var))))
3028 : {
3029 0 : if (TYPE_READONLY (TREE_TYPE (var)))
3030 0 : type = TREE_TYPE (var);
3031 : break;
3032 : }
3033 : }
3034 : }
3035 :
3036 14572 : if (type == error_mark_node)
3037 : return false;
3038 :
3039 : /* Variables with const-qualified type having no mutable member
3040 : are predetermined shared. */
3041 14557 : if (TYPE_READONLY (type) && !cp_has_mutable_p (type))
3042 69 : return true;
3043 :
3044 : return false;
3045 : }
3046 :
3047 : /* OMP_CLAUSE_DEFAULT_UNSPECIFIED unless OpenMP sharing attribute
3048 : of DECL is predetermined. */
3049 :
3050 : enum omp_clause_default_kind
3051 56058 : cxx_omp_predetermined_sharing_1 (tree decl)
3052 : {
3053 : /* Static data members are predetermined shared. */
3054 56058 : if (TREE_STATIC (decl))
3055 : {
3056 15253 : tree ctx = CP_DECL_CONTEXT (decl);
3057 15253 : if (TYPE_P (ctx) && MAYBE_CLASS_TYPE_P (ctx))
3058 : return OMP_CLAUSE_DEFAULT_SHARED;
3059 :
3060 15147 : if (c_omp_predefined_variable (decl))
3061 : return OMP_CLAUSE_DEFAULT_SHARED;
3062 : }
3063 :
3064 : /* this may not be specified in data-sharing clauses, still we need
3065 : to predetermined it firstprivate. */
3066 55907 : if (decl == current_class_ptr)
3067 113 : return OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
3068 :
3069 : return OMP_CLAUSE_DEFAULT_UNSPECIFIED;
3070 : }
3071 :
3072 : /* Likewise, but also include the artificial vars. We don't want to
3073 : disallow the artificial vars being mentioned in explicit clauses,
3074 : as we use artificial vars e.g. for loop constructs with random
3075 : access iterators other than pointers, but during gimplification
3076 : we want to treat them as predetermined. */
3077 :
3078 : enum omp_clause_default_kind
3079 35349 : cxx_omp_predetermined_sharing (tree decl)
3080 : {
3081 35349 : enum omp_clause_default_kind ret = cxx_omp_predetermined_sharing_1 (decl);
3082 35349 : if (ret != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
3083 : return ret;
3084 :
3085 : /* Predetermine artificial variables holding integral values, those
3086 : are usually result of gimplify_one_sizepos or SAVE_EXPR
3087 : gimplification. */
3088 35132 : if (VAR_P (decl)
3089 23189 : && DECL_ARTIFICIAL (decl)
3090 7202 : && INTEGRAL_TYPE_P (TREE_TYPE (decl))
3091 35656 : && !(DECL_LANG_SPECIFIC (decl)
3092 2 : && DECL_OMP_PRIVATIZED_MEMBER (decl)))
3093 : return OMP_CLAUSE_DEFAULT_SHARED;
3094 :
3095 : /* Similarly for typeinfo symbols. */
3096 34610 : if (VAR_P (decl) && DECL_ARTIFICIAL (decl) && DECL_TINFO_P (decl))
3097 60 : return OMP_CLAUSE_DEFAULT_SHARED;
3098 :
3099 : return OMP_CLAUSE_DEFAULT_UNSPECIFIED;
3100 : }
3101 :
3102 : enum omp_clause_defaultmap_kind
3103 17669 : cxx_omp_predetermined_mapping (tree decl)
3104 : {
3105 : /* Predetermine artificial variables holding integral values, those
3106 : are usually result of gimplify_one_sizepos or SAVE_EXPR
3107 : gimplification. */
3108 17669 : if (VAR_P (decl)
3109 1698 : && DECL_ARTIFICIAL (decl)
3110 142 : && INTEGRAL_TYPE_P (TREE_TYPE (decl))
3111 17735 : && !(DECL_LANG_SPECIFIC (decl)
3112 6 : && DECL_OMP_PRIVATIZED_MEMBER (decl)))
3113 : return OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
3114 :
3115 17603 : if (c_omp_predefined_variable (decl))
3116 12 : return OMP_CLAUSE_DEFAULTMAP_TO;
3117 :
3118 : return OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
3119 : }
3120 :
3121 : /* Finalize an implicitly determined clause. */
3122 :
3123 : void
3124 64413 : cxx_omp_finish_clause (tree c, gimple_seq *, bool /* openacc */)
3125 : {
3126 64413 : tree decl, inner_type;
3127 64413 : bool make_shared = false;
3128 :
3129 64413 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE
3130 56880 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_PRIVATE
3131 94501 : && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_LASTPRIVATE
3132 4752 : || !OMP_CLAUSE_LASTPRIVATE_LOOP_IV (c)))
3133 : return;
3134 :
3135 34340 : decl = OMP_CLAUSE_DECL (c);
3136 34340 : decl = require_complete_type (decl);
3137 34340 : inner_type = TREE_TYPE (decl);
3138 34340 : if (decl == error_mark_node)
3139 34340 : make_shared = true;
3140 34340 : else if (TYPE_REF_P (TREE_TYPE (decl)))
3141 86 : inner_type = TREE_TYPE (inner_type);
3142 :
3143 : /* We're interested in the base element, not arrays. */
3144 34610 : while (TREE_CODE (inner_type) == ARRAY_TYPE)
3145 270 : inner_type = TREE_TYPE (inner_type);
3146 :
3147 : /* Check for special function availability by building a call to one.
3148 : Save the results, because later we won't be in the right context
3149 : for making these queries. */
3150 34340 : bool first = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE;
3151 34340 : bool last = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE;
3152 34340 : if (!make_shared
3153 34340 : && CLASS_TYPE_P (inner_type)
3154 34564 : && cxx_omp_create_clause_info (c, inner_type, !first, first, last,
3155 : true))
3156 : make_shared = true;
3157 :
3158 34334 : if (make_shared)
3159 : {
3160 6 : OMP_CLAUSE_CODE (c) = OMP_CLAUSE_SHARED;
3161 6 : OMP_CLAUSE_SHARED_FIRSTPRIVATE (c) = 0;
3162 6 : OMP_CLAUSE_SHARED_READONLY (c) = 0;
3163 : }
3164 : }
3165 :
3166 : tree
3167 38 : cxx_omp_finish_mapper_clauses (tree clauses)
3168 : {
3169 38 : return finish_omp_clauses (clauses, C_ORT_OMP);
3170 : }
3171 :
3172 : /* Return true if DECL's DECL_VALUE_EXPR (if any) should be
3173 : disregarded in OpenMP construct, because it is going to be
3174 : remapped during OpenMP lowering. SHARED is true if DECL
3175 : is going to be shared, false if it is going to be privatized. */
3176 :
3177 : bool
3178 672950 : cxx_omp_disregard_value_expr (tree decl, bool shared)
3179 : {
3180 672950 : if (shared)
3181 : return false;
3182 404860 : if (VAR_P (decl)
3183 382649 : && DECL_HAS_VALUE_EXPR_P (decl)
3184 10096 : && DECL_ARTIFICIAL (decl)
3185 9066 : && DECL_LANG_SPECIFIC (decl)
3186 412787 : && DECL_OMP_PRIVATIZED_MEMBER (decl))
3187 : return true;
3188 399447 : if (VAR_P (decl) && DECL_CONTEXT (decl) && is_capture_proxy (decl))
3189 : return true;
3190 : return false;
3191 : }
3192 :
3193 : /* Fold any non-ODR-usages of a constant variable in expression X. */
3194 :
3195 : static tree
3196 585836699 : cp_fold_non_odr_use_1 (tree x)
3197 : {
3198 585836699 : tree var = x;
3199 883496728 : while (!VAR_P (var))
3200 808099620 : switch (TREE_CODE (var))
3201 : {
3202 285564910 : case ARRAY_REF:
3203 285564910 : case BIT_FIELD_REF:
3204 285564910 : case COMPONENT_REF:
3205 285564910 : case VIEW_CONVERT_EXPR:
3206 285564910 : CASE_CONVERT:
3207 285564910 : var = TREE_OPERAND (var, 0);
3208 285564910 : break;
3209 :
3210 28648978 : case INDIRECT_REF:
3211 28648978 : if (REFERENCE_REF_P (var))
3212 12095119 : var = TREE_OPERAND (var, 0);
3213 : else
3214 : return x;
3215 12095119 : break;
3216 :
3217 : default:
3218 : return x;
3219 : }
3220 :
3221 75397108 : if (TREE_THIS_VOLATILE (var)
3222 75397108 : || !decl_constant_var_p (var))
3223 : return x;
3224 :
3225 : /* We mustn't fold std::hardware_destructive_interference_size here
3226 : so that maybe_warn_about_constant_value can complain if it's used
3227 : in a manifestly constant-evaluated context. */
3228 5817950 : if (decl_in_std_namespace_p (var)
3229 2776462 : && DECL_NAME (var)
3230 8594412 : && id_equal (DECL_NAME (var), "hardware_destructive_interference_size"))
3231 : return x;
3232 :
3233 5817950 : tree t = maybe_constant_value (x);
3234 5817950 : return TREE_CONSTANT (t) ? t : x;
3235 : }
3236 :
3237 : /* Fold expression X which is used as an rvalue if RVAL is true. */
3238 :
3239 : static tree
3240 2588395852 : cp_fold_maybe_rvalue (tree x, bool rval, fold_flags_t flags)
3241 : {
3242 2609316874 : while (true)
3243 : {
3244 2598856363 : if (rval && (flags & ff_only_non_odr))
3245 569006998 : x = cp_fold_non_odr_use_1 (x);
3246 2598856363 : x = cp_fold (x, flags);
3247 2598856363 : if (rval)
3248 : {
3249 1736010323 : x = mark_rvalue_use (x);
3250 1736010323 : if (!(flags & ff_only_non_odr)
3251 1736010323 : && DECL_P (x) && !TYPE_REF_P (TREE_TYPE (x)))
3252 : {
3253 261183193 : tree v = decl_constant_value (x);
3254 261183193 : if (v != x && v != error_mark_node)
3255 : {
3256 10460511 : x = v;
3257 10460511 : continue;
3258 : }
3259 : }
3260 : }
3261 2588395852 : break;
3262 10460511 : }
3263 2588395852 : return x;
3264 : }
3265 :
3266 : tree
3267 76534952 : cp_fold_maybe_rvalue (tree x, bool rval)
3268 : {
3269 76534952 : return cp_fold_maybe_rvalue (x, rval, ff_none);
3270 : }
3271 :
3272 : /* Fold expression X which is used as an rvalue. */
3273 :
3274 : static tree
3275 274606691 : cp_fold_rvalue (tree x, fold_flags_t flags)
3276 : {
3277 10215329 : return cp_fold_maybe_rvalue (x, true, flags);
3278 : }
3279 :
3280 : tree
3281 2162457 : cp_fold_rvalue (tree x)
3282 : {
3283 2162457 : return cp_fold_rvalue (x, ff_none);
3284 : }
3285 :
3286 : /* Fold any non-ODR used constants in an expression X which
3287 : is used as an rvalue if RVAL is true. */
3288 :
3289 : tree
3290 736911 : cp_fold_non_odr_use (tree x, bool rval)
3291 : {
3292 736911 : return cp_fold_maybe_rvalue (x, rval, ff_only_non_odr);
3293 : }
3294 :
3295 : /* Perform folding on expression X. */
3296 :
3297 : static tree
3298 288075439 : cp_fully_fold (tree x, mce_value manifestly_const_eval)
3299 : {
3300 288075439 : if (processing_template_decl)
3301 : return x;
3302 : /* FIXME cp_fold ought to be a superset of maybe_constant_value so we don't
3303 : have to call both. */
3304 262228905 : if (cxx_dialect >= cxx11)
3305 : {
3306 261175118 : x = maybe_constant_value (x, /*decl=*/NULL_TREE, manifestly_const_eval);
3307 : /* Sometimes we are given a CONSTRUCTOR but the call above wraps it into
3308 : a TARGET_EXPR; undo that here. */
3309 261175118 : if (TREE_CODE (x) == TARGET_EXPR)
3310 681319 : x = TARGET_EXPR_INITIAL (x);
3311 260493799 : else if (TREE_CODE (x) == VIEW_CONVERT_EXPR
3312 27732357 : && TREE_CODE (TREE_OPERAND (x, 0)) == CONSTRUCTOR
3313 260493988 : && TREE_TYPE (TREE_OPERAND (x, 0)) == TREE_TYPE (x))
3314 189 : x = TREE_OPERAND (x, 0);
3315 : }
3316 262228905 : fold_flags_t flags = ff_none;
3317 262228905 : if (manifestly_const_eval == mce_false)
3318 45244557 : flags |= ff_mce_false;
3319 262228905 : return cp_fold_rvalue (x, flags);
3320 : }
3321 :
3322 : tree
3323 242830882 : cp_fully_fold (tree x)
3324 : {
3325 242830882 : return cp_fully_fold (x, mce_unknown);
3326 : }
3327 :
3328 : /* Likewise, but also fold recursively, which cp_fully_fold doesn't perform
3329 : in some cases. */
3330 :
3331 : tree
3332 47555604 : cp_fully_fold_init (tree x)
3333 : {
3334 47555604 : if (processing_template_decl)
3335 2311047 : return x;
3336 45244557 : x = cp_fully_fold (x, mce_false);
3337 45244557 : cp_fold_data data (ff_mce_false);
3338 45244557 : if (cxx_dialect >= cxx20)
3339 : {
3340 44610204 : cp_walk_tree (&x, cp_fold_immediate_r, &data, NULL);
3341 44610204 : data.pset.empty ();
3342 : }
3343 45244557 : cp_walk_tree (&x, cp_fold_r, &data, NULL);
3344 45244557 : return x;
3345 45244557 : }
3346 :
3347 : /* c-common interface to cp_fold. If IN_INIT, this is in a static initializer
3348 : and certain changes are made to the folding done. Or should be (FIXME). We
3349 : never touch maybe_const, as it is only used for the C front-end
3350 : C_MAYBE_CONST_EXPR. */
3351 :
3352 : tree
3353 76534952 : c_fully_fold (tree x, bool /*in_init*/, bool */*maybe_const*/, bool lval)
3354 : {
3355 76534952 : return cp_fold_maybe_rvalue (x, !lval);
3356 : }
3357 :
3358 : static GTY((deletable)) hash_map<tree, tree> *fold_caches[3];
3359 :
3360 : /* Subroutine of cp_fold. Returns which fold cache to use according
3361 : to the given flags. We need multiple caches since the result of
3362 : folding may depend on which flags are used. */
3363 :
3364 : static hash_map<tree, tree> *&
3365 4787769555 : get_fold_cache (fold_flags_t flags)
3366 : {
3367 0 : if (flags & ff_mce_false)
3368 2155280343 : return fold_caches[2];
3369 2632489212 : else if (flags & ff_only_non_odr)
3370 2267157254 : return fold_caches[1];
3371 : else
3372 365331958 : return fold_caches[0];
3373 : }
3374 :
3375 : /* Dispose of the whole FOLD_CACHE. */
3376 :
3377 : void
3378 40626104 : clear_fold_cache (void)
3379 : {
3380 162504416 : for (auto& fold_cache : fold_caches)
3381 121878312 : if (fold_cache != NULL)
3382 145963100 : fold_cache->empty ();
3383 40626104 : }
3384 :
3385 : /* This function tries to fold an expression X.
3386 : To avoid combinatorial explosion, folding results are kept in fold_cache.
3387 : If X is invalid, we don't fold at all.
3388 : For performance reasons we don't cache expressions representing a
3389 : declaration or constant.
3390 : Function returns X or its folded variant. */
3391 :
3392 : static tree
3393 7795544084 : cp_fold (tree x, fold_flags_t flags)
3394 : {
3395 7795544084 : tree op0, op1, op2, op3;
3396 7795544084 : tree org_x = x, r = NULL_TREE;
3397 7795544084 : enum tree_code code;
3398 7795544084 : location_t loc;
3399 7795544084 : bool rval_ops = true;
3400 :
3401 7795544084 : if (!x || x == error_mark_node)
3402 : return x;
3403 :
3404 7789042336 : if (EXPR_P (x) && (!TREE_TYPE (x) || TREE_TYPE (x) == error_mark_node))
3405 : return x;
3406 :
3407 : /* Don't bother to cache DECLs or constants. */
3408 7788741210 : if (DECL_P (x) || CONSTANT_CLASS_P (x))
3409 : return x;
3410 :
3411 4787769555 : auto& fold_cache = get_fold_cache (flags);
3412 4787769555 : if (fold_cache == NULL)
3413 514656 : fold_cache = hash_map<tree, tree>::create_ggc (101);
3414 :
3415 4787769555 : if (tree *cached = fold_cache->get (x))
3416 : {
3417 : /* unshare_expr doesn't recurse into SAVE_EXPRs. If SAVE_EXPR's
3418 : argument has been folded into a tree invariant, make sure it is
3419 : unshared. See PR112727. */
3420 1098892613 : if (TREE_CODE (x) == SAVE_EXPR && *cached != x)
3421 85 : return unshare_expr (*cached);
3422 1098892528 : return *cached;
3423 : }
3424 :
3425 3688876942 : uid_sensitive_constexpr_evaluation_checker c;
3426 :
3427 3688876942 : code = TREE_CODE (x);
3428 3688876942 : switch (code)
3429 : {
3430 209721998 : case CLEANUP_POINT_EXPR:
3431 : /* Strip CLEANUP_POINT_EXPR if the expression doesn't have side
3432 : effects. */
3433 209721998 : r = cp_fold (TREE_OPERAND (x, 0), flags);
3434 209721998 : if (!TREE_SIDE_EFFECTS (r) && !(flags & ff_only_non_odr))
3435 2383087 : x = r;
3436 : break;
3437 :
3438 1697232 : case SIZEOF_EXPR:
3439 1697232 : x = fold_sizeof_expr (x);
3440 1697232 : break;
3441 :
3442 328807503 : case VIEW_CONVERT_EXPR:
3443 328807503 : rval_ops = false;
3444 : /* FALLTHRU */
3445 1012099453 : case NON_LVALUE_EXPR:
3446 1012099453 : CASE_CONVERT:
3447 :
3448 1012099453 : if (VOID_TYPE_P (TREE_TYPE (x)))
3449 : {
3450 : /* This is just to make sure we don't end up with casts to
3451 : void from error_mark_node. If we just return x, then
3452 : cp_fold_r might fold the operand into error_mark_node and
3453 : leave the conversion in the IR. STRIP_USELESS_TYPE_CONVERSION
3454 : during gimplification doesn't like such casts.
3455 : Don't create a new tree if op0 != TREE_OPERAND (x, 0), the
3456 : folding of the operand should be in the caches and if in cp_fold_r
3457 : it will modify it in place. */
3458 92856901 : op0 = cp_fold (TREE_OPERAND (x, 0), flags);
3459 92856901 : if (op0 == error_mark_node)
3460 119 : x = error_mark_node;
3461 : break;
3462 : }
3463 :
3464 919242552 : loc = EXPR_LOCATION (x);
3465 919242552 : op0 = cp_fold_maybe_rvalue (TREE_OPERAND (x, 0), rval_ops, flags);
3466 :
3467 919242552 : if (op0 == error_mark_node)
3468 3 : x = error_mark_node;
3469 919242549 : else if (flags & ff_only_non_odr)
3470 : {
3471 342030207 : if (op0 != TREE_OPERAND (x, 0))
3472 2086979 : x = build1_loc (loc, code, TREE_TYPE (x), op0);
3473 342030207 : if (code == NOP_EXPR)
3474 174508748 : REINTERPRET_CAST_P (x) = REINTERPRET_CAST_P (org_x);
3475 : }
3476 577212342 : else if (code == CONVERT_EXPR
3477 45656518 : && SCALAR_TYPE_P (TREE_TYPE (x))
3478 622867832 : && op0 != void_node)
3479 : /* During parsing we used convert_to_*_nofold; re-convert now using the
3480 : folding variants, since fold() doesn't do those transformations. */
3481 41259841 : x = fold (convert (TREE_TYPE (x), op0));
3482 535952501 : else if (op0 != TREE_OPERAND (x, 0))
3483 142491935 : x = fold_build1_loc (loc, code, TREE_TYPE (x), op0);
3484 : else
3485 393460566 : x = fold (x);
3486 :
3487 : /* Conversion of an out-of-range value has implementation-defined
3488 : behavior; the language considers it different from arithmetic
3489 : overflow, which is undefined. */
3490 919242552 : if (TREE_CODE (op0) == INTEGER_CST
3491 919242552 : && TREE_OVERFLOW_P (x) && !TREE_OVERFLOW_P (op0))
3492 44 : TREE_OVERFLOW (x) = false;
3493 :
3494 : break;
3495 :
3496 261 : case EXCESS_PRECISION_EXPR:
3497 261 : op0 = cp_fold_maybe_rvalue (TREE_OPERAND (x, 0), rval_ops, flags);
3498 261 : if (op0 == error_mark_node)
3499 0 : x = error_mark_node;
3500 261 : else if (flags & ff_only_non_odr)
3501 : {
3502 67 : if (op0 != TREE_OPERAND (x, 0))
3503 0 : x = build1_loc (EXPR_LOCATION (x), code, TREE_TYPE (x), op0);
3504 : }
3505 : else
3506 194 : x = fold_convert_loc (EXPR_LOCATION (x), TREE_TYPE (x), op0);
3507 : break;
3508 :
3509 126575324 : case INDIRECT_REF:
3510 : /* We don't need the decltype(auto) obfuscation anymore. */
3511 126575324 : if (REF_PARENTHESIZED_P (x))
3512 : {
3513 855 : tree p = maybe_undo_parenthesized_ref (x);
3514 855 : if (p != x)
3515 0 : return cp_fold (p, flags);
3516 : }
3517 : /* When folding non-ODR usages of constants, we also want to
3518 : remove any constant-initialized references, even when
3519 : used as lvalues. */
3520 126575324 : if ((flags & ff_only_non_odr) && REFERENCE_REF_P (x))
3521 : {
3522 16829701 : op0 = cp_fold_non_odr_use_1 (TREE_OPERAND (x, 0));
3523 16829701 : if (op0 != TREE_OPERAND (x, 0))
3524 1575 : return convert_from_reference (cp_fold (op0, flags));
3525 : }
3526 126573749 : goto unary;
3527 :
3528 317022259 : case ADDR_EXPR:
3529 317022259 : loc = EXPR_LOCATION (x);
3530 317022259 : op0 = cp_fold_maybe_rvalue (TREE_OPERAND (x, 0), false, flags);
3531 :
3532 : /* Cope with user tricks that amount to offsetof. */
3533 317022259 : if (op0 != error_mark_node
3534 317022259 : && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (op0))
3535 435125760 : && !(flags & ff_only_non_odr))
3536 : {
3537 65522552 : tree val = get_base_address (op0);
3538 65522552 : if (val
3539 65522552 : && INDIRECT_REF_P (val)
3540 25417413 : && COMPLETE_TYPE_P (TREE_TYPE (val))
3541 90939875 : && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3542 : {
3543 261 : val = TREE_OPERAND (val, 0);
3544 261 : STRIP_NOPS (val);
3545 261 : val = maybe_constant_value (val);
3546 261 : if (TREE_CODE (val) == INTEGER_CST)
3547 127 : return fold_offsetof (op0, TREE_TYPE (x));
3548 : }
3549 : }
3550 317022132 : goto finish_unary;
3551 :
3552 : case REALPART_EXPR:
3553 : case IMAGPART_EXPR:
3554 169414966 : rval_ops = false;
3555 : /* FALLTHRU */
3556 169414966 : case CONJ_EXPR:
3557 169414966 : case FIX_TRUNC_EXPR:
3558 169414966 : case FLOAT_EXPR:
3559 169414966 : case NEGATE_EXPR:
3560 169414966 : case ABS_EXPR:
3561 169414966 : case ABSU_EXPR:
3562 169414966 : case BIT_NOT_EXPR:
3563 169414966 : case TRUTH_NOT_EXPR:
3564 169414966 : case FIXED_CONVERT_EXPR:
3565 169414966 : unary:
3566 :
3567 169414966 : loc = EXPR_LOCATION (x);
3568 169414966 : op0 = cp_fold_maybe_rvalue (TREE_OPERAND (x, 0), rval_ops, flags);
3569 :
3570 486437098 : finish_unary:
3571 486437098 : if (op0 == error_mark_node)
3572 3 : x = error_mark_node;
3573 486437095 : else if (op0 != TREE_OPERAND (x, 0))
3574 : {
3575 31541397 : if (flags & ff_only_non_odr)
3576 420218 : x = build1_loc (loc, code, TREE_TYPE (x), op0);
3577 : else
3578 31121179 : x = fold_build1_loc (loc, code, TREE_TYPE (x), op0);
3579 31541397 : if (code == INDIRECT_REF
3580 9627618 : && (INDIRECT_REF_P (x) || TREE_CODE (x) == MEM_REF))
3581 : {
3582 9627413 : TREE_READONLY (x) = TREE_READONLY (org_x);
3583 9627413 : TREE_SIDE_EFFECTS (x) = TREE_SIDE_EFFECTS (org_x);
3584 9627413 : TREE_THIS_VOLATILE (x) = TREE_THIS_VOLATILE (org_x);
3585 : }
3586 : }
3587 454895698 : else if (!(flags & ff_only_non_odr))
3588 224852446 : x = fold (x);
3589 :
3590 486437098 : gcc_assert (TREE_CODE (x) != COND_EXPR
3591 : || !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))));
3592 : break;
3593 :
3594 318180 : case UNARY_PLUS_EXPR:
3595 318180 : op0 = cp_fold_rvalue (TREE_OPERAND (x, 0), flags);
3596 318180 : if (op0 == error_mark_node)
3597 0 : x = error_mark_node;
3598 318180 : else if (flags & ff_only_non_odr)
3599 : {
3600 125780 : if (op0 != TREE_OPERAND (x, 0))
3601 12 : x = build1_loc (EXPR_LOCATION (x), code, TREE_TYPE (x), op0);
3602 : }
3603 : else
3604 192400 : x = fold_convert (TREE_TYPE (x), op0);
3605 : break;
3606 :
3607 200072783 : case POSTDECREMENT_EXPR:
3608 200072783 : case POSTINCREMENT_EXPR:
3609 200072783 : case INIT_EXPR:
3610 200072783 : case PREDECREMENT_EXPR:
3611 200072783 : case PREINCREMENT_EXPR:
3612 200072783 : case COMPOUND_EXPR:
3613 200072783 : case MODIFY_EXPR:
3614 200072783 : rval_ops = false;
3615 : /* FALLTHRU */
3616 415418630 : case POINTER_PLUS_EXPR:
3617 415418630 : case PLUS_EXPR:
3618 415418630 : case POINTER_DIFF_EXPR:
3619 415418630 : case MINUS_EXPR:
3620 415418630 : case MULT_EXPR:
3621 415418630 : case TRUNC_DIV_EXPR:
3622 415418630 : case CEIL_DIV_EXPR:
3623 415418630 : case FLOOR_DIV_EXPR:
3624 415418630 : case ROUND_DIV_EXPR:
3625 415418630 : case TRUNC_MOD_EXPR:
3626 415418630 : case CEIL_MOD_EXPR:
3627 415418630 : case ROUND_MOD_EXPR:
3628 415418630 : case RDIV_EXPR:
3629 415418630 : case EXACT_DIV_EXPR:
3630 415418630 : case MIN_EXPR:
3631 415418630 : case MAX_EXPR:
3632 415418630 : case LSHIFT_EXPR:
3633 415418630 : case RSHIFT_EXPR:
3634 415418630 : case LROTATE_EXPR:
3635 415418630 : case RROTATE_EXPR:
3636 415418630 : case BIT_AND_EXPR:
3637 415418630 : case BIT_IOR_EXPR:
3638 415418630 : case BIT_XOR_EXPR:
3639 415418630 : case TRUTH_AND_EXPR:
3640 415418630 : case TRUTH_ANDIF_EXPR:
3641 415418630 : case TRUTH_OR_EXPR:
3642 415418630 : case TRUTH_ORIF_EXPR:
3643 415418630 : case TRUTH_XOR_EXPR:
3644 415418630 : case LT_EXPR: case LE_EXPR:
3645 415418630 : case GT_EXPR: case GE_EXPR:
3646 415418630 : case EQ_EXPR: case NE_EXPR:
3647 415418630 : case UNORDERED_EXPR: case ORDERED_EXPR:
3648 415418630 : case UNLT_EXPR: case UNLE_EXPR:
3649 415418630 : case UNGT_EXPR: case UNGE_EXPR:
3650 415418630 : case UNEQ_EXPR: case LTGT_EXPR:
3651 415418630 : case RANGE_EXPR: case COMPLEX_EXPR:
3652 :
3653 415418630 : loc = EXPR_LOCATION (x);
3654 415418630 : op0 = cp_fold_maybe_rvalue (TREE_OPERAND (x, 0), rval_ops, flags);
3655 415418630 : bool clear_decl_read;
3656 415418630 : clear_decl_read = false;
3657 415418630 : if (code == MODIFY_EXPR
3658 52275470 : && (VAR_P (op0) || TREE_CODE (op0) == PARM_DECL)
3659 430131061 : && !DECL_READ_P (op0))
3660 : clear_decl_read = true;
3661 415418630 : op1 = cp_fold_maybe_rvalue (TREE_OPERAND (x, 1),
3662 : code != COMPOUND_EXPR, flags);
3663 415418630 : if (clear_decl_read)
3664 769483 : DECL_READ_P (op0) = 0;
3665 :
3666 415418630 : if (flags & ff_only_non_odr)
3667 : {
3668 180848481 : if (op0 == error_mark_node || op1 == error_mark_node)
3669 24 : x = error_mark_node;
3670 180848457 : else if (op0 != TREE_OPERAND (x, 0) || op1 != TREE_OPERAND (x, 1))
3671 : {
3672 7988869 : if (code == INIT_EXPR && op1 != TREE_OPERAND (x, 1))
3673 2505010 : set_target_expr_eliding (op1);
3674 7988869 : x = build2_loc (loc, code, TREE_TYPE (x), op0, op1);
3675 : }
3676 : break;
3677 : }
3678 :
3679 : /* decltype(nullptr) has only one value, so optimize away all comparisons
3680 : with that type right away, keeping them in the IL causes troubles for
3681 : various optimizations. */
3682 234570149 : if (COMPARISON_CLASS_P (org_x)
3683 38904149 : && TREE_CODE (TREE_TYPE (op0)) == NULLPTR_TYPE
3684 234570176 : && TREE_CODE (TREE_TYPE (op1)) == NULLPTR_TYPE)
3685 : {
3686 27 : switch (code)
3687 : {
3688 12 : case EQ_EXPR:
3689 12 : x = constant_boolean_node (true, TREE_TYPE (x));
3690 12 : break;
3691 15 : case NE_EXPR:
3692 15 : x = constant_boolean_node (false, TREE_TYPE (x));
3693 15 : break;
3694 0 : default:
3695 0 : gcc_unreachable ();
3696 : }
3697 27 : return omit_two_operands_loc (loc, TREE_TYPE (x), x,
3698 27 : op0, op1);
3699 : }
3700 :
3701 234570122 : if (op0 == error_mark_node || op1 == error_mark_node)
3702 105 : x = error_mark_node;
3703 234570017 : else if (op0 != TREE_OPERAND (x, 0) || op1 != TREE_OPERAND (x, 1))
3704 161296607 : x = fold_build2_loc (loc, code, TREE_TYPE (x), op0, op1);
3705 : else
3706 73273410 : x = fold (x);
3707 :
3708 : /* This is only needed for -Wnonnull-compare and only if
3709 : TREE_NO_WARNING (org_x), but to avoid that option affecting code
3710 : generation, we do it always. */
3711 234570122 : if (COMPARISON_CLASS_P (org_x))
3712 : {
3713 38904122 : if (x == error_mark_node || TREE_CODE (x) == INTEGER_CST)
3714 : ;
3715 37787450 : else if (COMPARISON_CLASS_P (x))
3716 : {
3717 36771050 : if (warn_nonnull_compare
3718 36771050 : && warning_suppressed_p (org_x, OPT_Wnonnull_compare))
3719 122805 : suppress_warning (x, OPT_Wnonnull_compare);
3720 : }
3721 : /* Otherwise give up on optimizing these, let GIMPLE folders
3722 : optimize those later on. */
3723 1016400 : else if (op0 != TREE_OPERAND (org_x, 0)
3724 1016400 : || op1 != TREE_OPERAND (org_x, 1))
3725 : {
3726 1014337 : x = build2_loc (loc, code, TREE_TYPE (org_x), op0, op1);
3727 1014337 : if (warn_nonnull_compare
3728 1014337 : && warning_suppressed_p (org_x, OPT_Wnonnull_compare))
3729 16 : suppress_warning (x, OPT_Wnonnull_compare);
3730 : }
3731 : else
3732 2063 : x = org_x;
3733 : }
3734 :
3735 : break;
3736 :
3737 9897149 : case VEC_COND_EXPR:
3738 9897149 : case COND_EXPR:
3739 9897149 : loc = EXPR_LOCATION (x);
3740 9897149 : op0 = cp_fold_rvalue (TREE_OPERAND (x, 0), flags);
3741 9897149 : op1 = cp_fold (TREE_OPERAND (x, 1), flags);
3742 9897149 : op2 = cp_fold (TREE_OPERAND (x, 2), flags);
3743 :
3744 9897149 : if (flags & ff_only_non_odr)
3745 : {
3746 4166696 : if (op0 == error_mark_node
3747 4166690 : || op1 == error_mark_node
3748 4166690 : || op2 == error_mark_node)
3749 6 : x = error_mark_node;
3750 4166690 : else if (op0 != TREE_OPERAND (x, 0)
3751 4089321 : || op1 != TREE_OPERAND (x, 1)
3752 8032072 : || op2 != TREE_OPERAND (x, 2))
3753 316220 : x = build3_loc (loc, code, TREE_TYPE (x), op0, op1, op2);
3754 : break;
3755 : }
3756 :
3757 5730453 : if (TREE_CODE (TREE_TYPE (x)) == BOOLEAN_TYPE)
3758 : {
3759 20959 : warning_sentinel s (warn_int_in_bool_context);
3760 20959 : if (!VOID_TYPE_P (TREE_TYPE (op1)))
3761 20959 : op1 = cp_truthvalue_conversion (op1, tf_warning_or_error);
3762 20959 : if (!VOID_TYPE_P (TREE_TYPE (op2)))
3763 20938 : op2 = cp_truthvalue_conversion (op2, tf_warning_or_error);
3764 20959 : }
3765 5709494 : else if (VOID_TYPE_P (TREE_TYPE (x)))
3766 : {
3767 1870300 : if (TREE_CODE (op0) == INTEGER_CST)
3768 : {
3769 : /* If the condition is constant, fold can fold away
3770 : the COND_EXPR. If some statement-level uses of COND_EXPR
3771 : have one of the branches NULL, avoid folding crash. */
3772 341964 : if (!op1)
3773 0 : op1 = build_empty_stmt (loc);
3774 341964 : if (!op2)
3775 12 : op2 = build_empty_stmt (loc);
3776 : }
3777 : else
3778 : {
3779 : /* Otherwise, don't bother folding a void condition, since
3780 : it can't produce a constant value. */
3781 1528336 : if (op0 != TREE_OPERAND (x, 0)
3782 1436905 : || op1 != TREE_OPERAND (x, 1)
3783 2600360 : || op2 != TREE_OPERAND (x, 2))
3784 458558 : x = build3_loc (loc, code, TREE_TYPE (x), op0, op1, op2);
3785 : break;
3786 : }
3787 : }
3788 :
3789 4202117 : if (op0 == error_mark_node
3790 4202117 : || op1 == error_mark_node
3791 4202103 : || op2 == error_mark_node)
3792 62 : x = error_mark_node;
3793 4202055 : else if (op0 != TREE_OPERAND (x, 0)
3794 1704958 : || op1 != TREE_OPERAND (x, 1)
3795 5537197 : || op2 != TREE_OPERAND (x, 2))
3796 2974207 : x = fold_build3_loc (loc, code, TREE_TYPE (x), op0, op1, op2);
3797 : else
3798 1227848 : x = fold (x);
3799 :
3800 : /* A COND_EXPR might have incompatible types in branches if one or both
3801 : arms are bitfields. If folding exposed such a branch, fix it up. */
3802 4202117 : if (TREE_CODE (x) != code
3803 967726 : && x != error_mark_node
3804 5169781 : && !useless_type_conversion_p (TREE_TYPE (org_x), TREE_TYPE (x)))
3805 17489 : x = fold_convert (TREE_TYPE (org_x), x);
3806 :
3807 : break;
3808 :
3809 230266680 : case CALL_EXPR:
3810 230266680 : {
3811 230266680 : tree callee = get_callee_fndecl (x);
3812 :
3813 : /* "Inline" calls to std::move/forward and other cast-like functions
3814 : by simply folding them into a corresponding cast to their return
3815 : type. This is cheaper than relying on the middle end to do so, and
3816 : also means we avoid generating useless debug info for them at all.
3817 :
3818 : At this point the argument has already been converted into a
3819 : reference, so it suffices to use a NOP_EXPR to express the
3820 : cast. */
3821 230266680 : if ((OPTION_SET_P (flag_fold_simple_inlines)
3822 230266680 : ? flag_fold_simple_inlines
3823 230266365 : : !flag_no_inline)
3824 217670660 : && !(flags & ff_only_non_odr)
3825 125767991 : && call_expr_nargs (x) == 1
3826 66287190 : && decl_in_std_namespace_p (callee)
3827 42358827 : && DECL_NAME (callee) != NULL_TREE
3828 272625507 : && (id_equal (DECL_NAME (callee), "move")
3829 41044723 : || id_equal (DECL_NAME (callee), "forward")
3830 39435021 : || id_equal (DECL_NAME (callee), "forward_like")
3831 39434875 : || id_equal (DECL_NAME (callee), "addressof")
3832 : /* This addressof equivalent is used heavily in libstdc++. */
3833 39103639 : || id_equal (DECL_NAME (callee), "__addressof")
3834 38718930 : || id_equal (DECL_NAME (callee), "to_underlying")
3835 38718924 : || id_equal (DECL_NAME (callee), "as_const")))
3836 : {
3837 3642506 : r = CALL_EXPR_ARG (x, 0);
3838 : /* These type-checks must be performed here, because invalid
3839 : definitions of these functions could fail to ensure those and
3840 : build_nop could misbehave. See PR122185. */
3841 3642506 : if (id_equal (DECL_NAME (callee), "to_underlying")
3842 3642506 : ? TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
3843 6 : && INTEGRAL_TYPE_P (TREE_TYPE (x))
3844 6569055 : : INDIRECT_TYPE_P (TREE_TYPE (x))
3845 6569043 : && INDIRECT_TYPE_P (TREE_TYPE (r)))
3846 : {
3847 3642491 : r = build_nop (TREE_TYPE (x), r);
3848 3642491 : x = cp_fold (r, flags);
3849 : }
3850 : break;
3851 : }
3852 :
3853 226624174 : int sv = optimize, nw = sv;
3854 :
3855 : /* Some built-in function calls will be evaluated at compile-time in
3856 : fold (). Set optimize to 1 when folding __builtin_constant_p inside
3857 : a constexpr function so that fold_builtin_1 doesn't fold it to 0. */
3858 224221916 : if (callee && fndecl_built_in_p (callee) && !optimize
3859 2137919 : && DECL_IS_BUILTIN_CONSTANT_P (callee)
3860 50322 : && current_function_decl
3861 226674480 : && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
3862 : nw = 1;
3863 :
3864 224221916 : if (callee && !(flags & ff_only_non_odr)
3865 354531480 : && fndecl_built_in_p (callee, BUILT_IN_FRONTEND))
3866 : {
3867 60547 : iloc_sentinel ils (EXPR_LOCATION (x));
3868 60547 : switch (DECL_FE_FUNCTION_CODE (callee))
3869 : {
3870 53696 : case CP_BUILT_IN_IS_CONSTANT_EVALUATED:
3871 : /* Defer folding __builtin_is_constant_evaluated unless
3872 : we know this isn't a manifestly constant-evaluated
3873 : context. */
3874 53696 : if (flags & ff_mce_false)
3875 27229 : x = boolean_false_node;
3876 : break;
3877 3 : case CP_BUILT_IN_SOURCE_LOCATION:
3878 3 : x = fold_builtin_source_location (x);
3879 3 : break;
3880 456 : case CP_BUILT_IN_IS_CORRESPONDING_MEMBER:
3881 912 : x = fold_builtin_is_corresponding_member
3882 456 : (EXPR_LOCATION (x), call_expr_nargs (x),
3883 : &CALL_EXPR_ARG (x, 0));
3884 456 : break;
3885 400 : case CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS:
3886 800 : x = fold_builtin_is_pointer_inverconvertible_with_class
3887 400 : (EXPR_LOCATION (x), call_expr_nargs (x),
3888 : &CALL_EXPR_ARG (x, 0));
3889 400 : break;
3890 : default:
3891 : break;
3892 : }
3893 60547 : break;
3894 60547 : }
3895 :
3896 226563627 : bool changed = false;
3897 226563627 : int m = call_expr_nargs (x);
3898 558740708 : for (int i = 0; i < m; i++)
3899 : {
3900 332177081 : r = cp_fold (CALL_EXPR_ARG (x, i), flags);
3901 332177081 : if (r != CALL_EXPR_ARG (x, i))
3902 : {
3903 109237042 : if (r == error_mark_node)
3904 : {
3905 0 : x = error_mark_node;
3906 0 : break;
3907 : }
3908 109237042 : if (!changed)
3909 69651513 : x = copy_node (x);
3910 109237042 : CALL_EXPR_ARG (x, i) = r;
3911 109237042 : changed = true;
3912 : }
3913 : }
3914 : /* Don't fold away the function entirely if we're just folding
3915 : non-ODR-used variables. */
3916 226563627 : if (x == error_mark_node || (flags & ff_only_non_odr))
3917 : break;
3918 :
3919 129234361 : optimize = nw;
3920 129234361 : r = fold (x);
3921 129234361 : optimize = sv;
3922 :
3923 129234361 : if (TREE_CODE (r) != CALL_EXPR)
3924 : {
3925 2945904 : x = cp_fold (r, flags);
3926 2945904 : break;
3927 : }
3928 :
3929 126288457 : optimize = nw;
3930 :
3931 : /* Invoke maybe_constant_value for functions declared
3932 : constexpr and not called with AGGR_INIT_EXPRs.
3933 : TODO:
3934 : Do constexpr expansion of expressions where the call itself is not
3935 : constant, but the call followed by an INDIRECT_REF is. */
3936 124900855 : if (callee && DECL_DECLARED_CONSTEXPR_P (callee)
3937 : && !(flags & ff_only_non_odr)
3938 188718779 : && (!flag_no_inline
3939 3758835 : || lookup_attribute ("always_inline",
3940 3758835 : DECL_ATTRIBUTES (callee))))
3941 : {
3942 59508409 : mce_value manifestly_const_eval = mce_unknown;
3943 59508409 : if (flags & ff_mce_false)
3944 : /* Allow folding __builtin_is_constant_evaluated to false during
3945 : constexpr evaluation of this call. */
3946 46155589 : manifestly_const_eval = mce_false;
3947 59508409 : r = maybe_constant_value (x, /*decl=*/NULL_TREE,
3948 : manifestly_const_eval);
3949 : }
3950 126288457 : optimize = sv;
3951 :
3952 126288457 : if (TREE_CODE (r) != CALL_EXPR)
3953 : {
3954 9045014 : if (DECL_CONSTRUCTOR_P (callee))
3955 489 : r = cp_build_init_expr_for_ctor (x, r);
3956 4522507 : x = r;
3957 4522507 : break;
3958 : }
3959 :
3960 : break;
3961 : }
3962 :
3963 24420159 : case CONSTRUCTOR:
3964 24420159 : {
3965 24420159 : unsigned i;
3966 24420159 : constructor_elt *p;
3967 24420159 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (x);
3968 24420159 : vec<constructor_elt, va_gc> *nelts = NULL;
3969 116524096 : FOR_EACH_VEC_SAFE_ELT (elts, i, p)
3970 : {
3971 80959687 : tree op = cp_fold (p->value, flags);
3972 80959687 : if (op == error_mark_node)
3973 : {
3974 0 : x = error_mark_node;
3975 0 : vec_free (nelts);
3976 : break;
3977 : }
3978 80959687 : else if (op != p->value)
3979 : {
3980 1251357 : if (nelts == NULL)
3981 804014 : nelts = elts->copy ();
3982 1251357 : (*nelts)[i].value = op;
3983 : }
3984 : }
3985 24420159 : if (nelts)
3986 : {
3987 804014 : x = build_constructor (TREE_TYPE (x), nelts);
3988 804014 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY (x)
3989 804014 : = CONSTRUCTOR_PLACEHOLDER_BOUNDARY (org_x);
3990 804014 : CONSTRUCTOR_MUTABLE_POISON (x)
3991 1608028 : = CONSTRUCTOR_MUTABLE_POISON (org_x);
3992 : }
3993 24420159 : if (VECTOR_TYPE_P (TREE_TYPE (x)))
3994 78700 : x = fold (x);
3995 : break;
3996 : }
3997 832120 : case TREE_VEC:
3998 832120 : {
3999 832120 : bool changed = false;
4000 832120 : int n = TREE_VEC_LENGTH (x);
4001 :
4002 2094944 : for (int i = 0; i < n; i++)
4003 : {
4004 1262824 : tree op = cp_fold (TREE_VEC_ELT (x, i), flags);
4005 1262824 : if (op != TREE_VEC_ELT (x, i))
4006 : {
4007 865 : if (!changed)
4008 822 : x = copy_node (x);
4009 865 : TREE_VEC_ELT (x, i) = op;
4010 865 : changed = true;
4011 : }
4012 : }
4013 : }
4014 :
4015 : break;
4016 :
4017 3236875 : case ARRAY_REF:
4018 3236875 : case ARRAY_RANGE_REF:
4019 :
4020 3236875 : loc = EXPR_LOCATION (x);
4021 3236875 : op0 = cp_fold (TREE_OPERAND (x, 0), flags);
4022 3236875 : op1 = cp_fold (TREE_OPERAND (x, 1), flags);
4023 3236875 : op2 = cp_fold (TREE_OPERAND (x, 2), flags);
4024 3236875 : op3 = cp_fold (TREE_OPERAND (x, 3), flags);
4025 :
4026 3236875 : if (op0 == error_mark_node
4027 3236875 : || op1 == error_mark_node
4028 3236875 : || op2 == error_mark_node
4029 3236875 : || op3 == error_mark_node)
4030 0 : x = error_mark_node;
4031 3236875 : else if (op0 != TREE_OPERAND (x, 0)
4032 2225626 : || op1 != TREE_OPERAND (x, 1)
4033 1727620 : || op2 != TREE_OPERAND (x, 2)
4034 4964495 : || op3 != TREE_OPERAND (x, 3))
4035 : {
4036 1509255 : x = build4_loc (loc, code, TREE_TYPE (x), op0, op1, op2, op3);
4037 1509255 : TREE_READONLY (x) = TREE_READONLY (org_x);
4038 1509255 : TREE_SIDE_EFFECTS (x) = TREE_SIDE_EFFECTS (org_x);
4039 1509255 : TREE_THIS_VOLATILE (x) = TREE_THIS_VOLATILE (org_x);
4040 : }
4041 :
4042 3236875 : if (!(flags & ff_only_non_odr))
4043 1884142 : x = fold (x);
4044 : break;
4045 :
4046 1707891 : case SAVE_EXPR:
4047 : /* A SAVE_EXPR might contain e.g. (0 * i) + (0 * j), which, after
4048 : folding, evaluates to an invariant. In that case no need to wrap
4049 : this folded tree with a SAVE_EXPR. */
4050 1707891 : r = cp_fold (TREE_OPERAND (x, 0), flags);
4051 1707891 : if (tree_invariant_p (r))
4052 57 : x = r;
4053 : break;
4054 :
4055 16 : case REQUIRES_EXPR:
4056 16 : x = evaluate_requires_expr (x);
4057 16 : break;
4058 :
4059 : default:
4060 : return org_x;
4061 : }
4062 :
4063 2396053715 : if (EXPR_P (x) && TREE_CODE (x) == code)
4064 : {
4065 1993076886 : TREE_THIS_VOLATILE (x) = TREE_THIS_VOLATILE (org_x);
4066 1993076886 : copy_warning (x, org_x);
4067 : }
4068 :
4069 2396053715 : if (!c.evaluation_restricted_p ())
4070 : {
4071 2396008649 : fold_cache->put (org_x, x);
4072 : /* Prevent that we try to fold an already folded result again. */
4073 2396008649 : if (x != org_x)
4074 752081703 : fold_cache->put (x, x);
4075 : }
4076 :
4077 : return x;
4078 : }
4079 :
4080 : /* Look up "hot", "cold", "likely" or "unlikely" in attribute list LIST. */
4081 :
4082 : tree
4083 311310023 : lookup_hotness_attribute (tree list)
4084 : {
4085 311417686 : for (; list; list = TREE_CHAIN (list))
4086 : {
4087 2021197 : tree name = get_attribute_name (list);
4088 2021197 : if ((is_attribute_p ("hot", name)
4089 2021197 : || is_attribute_p ("cold", name)
4090 2021194 : || is_attribute_p ("likely", name)
4091 1391242 : || is_attribute_p ("unlikely", name))
4092 3934740 : && is_attribute_namespace_p ("", list))
4093 : break;
4094 : }
4095 311310023 : return list;
4096 : }
4097 :
4098 : /* Remove "hot", "cold", "likely" and "unlikely" attributes from LIST. */
4099 :
4100 : static tree
4101 1913525 : remove_hotness_attribute (tree list)
4102 : {
4103 3827068 : for (tree *p = &list; *p; )
4104 : {
4105 1913543 : tree l = *p;
4106 1913543 : tree name = get_attribute_name (l);
4107 1913543 : if ((is_attribute_p ("hot", name)
4108 1913543 : || is_attribute_p ("cold", name)
4109 1913540 : || is_attribute_p ("likely", name)
4110 1283588 : || is_attribute_p ("unlikely", name))
4111 3827086 : && is_attribute_namespace_p ("", l))
4112 : {
4113 1913534 : *p = TREE_CHAIN (l);
4114 1913534 : continue;
4115 : }
4116 9 : p = &TREE_CHAIN (l);
4117 : }
4118 1913525 : return list;
4119 : }
4120 :
4121 : /* If [[likely]] or [[unlikely]] appear on this statement, turn it into a
4122 : PREDICT_EXPR. */
4123 :
4124 : tree
4125 309396516 : process_stmt_hotness_attribute (tree std_attrs, location_t attrs_loc)
4126 : {
4127 309396516 : if (std_attrs == error_mark_node)
4128 : return std_attrs;
4129 309396498 : if (tree attr = lookup_hotness_attribute (std_attrs))
4130 : {
4131 1913525 : tree name = get_attribute_name (attr);
4132 1913525 : bool hot = (is_attribute_p ("hot", name)
4133 1913525 : || is_attribute_p ("likely", name));
4134 1913525 : tree pred = build_predict_expr (hot ? PRED_HOT_LABEL : PRED_COLD_LABEL,
4135 : hot ? TAKEN : NOT_TAKEN);
4136 1913525 : SET_EXPR_LOCATION (pred, attrs_loc);
4137 1913525 : add_stmt (pred);
4138 1913525 : if (tree other = lookup_hotness_attribute (TREE_CHAIN (attr)))
4139 : {
4140 9 : auto_urlify_attributes sentinel;
4141 9 : warning (OPT_Wattributes, "ignoring attribute %qE after earlier %qE",
4142 : get_attribute_name (other), name);
4143 9 : }
4144 1913525 : std_attrs = remove_hotness_attribute (std_attrs);
4145 : }
4146 : return std_attrs;
4147 : }
4148 :
4149 : /* Build IFN_ASSUME internal call for assume condition ARG. */
4150 :
4151 : tree
4152 11770 : build_assume_call (location_t loc, tree arg)
4153 : {
4154 11770 : if (!processing_template_decl)
4155 11669 : arg = fold_build_cleanup_point_expr (TREE_TYPE (arg), arg);
4156 11770 : return build_call_expr_internal_loc (loc, IFN_ASSUME, void_type_node,
4157 11770 : 1, arg);
4158 : }
4159 :
4160 : /* If [[assume (cond)]] appears on this statement, handle it. */
4161 :
4162 : tree
4163 243615546 : process_stmt_assume_attribute (tree std_attrs, tree statement,
4164 : location_t attrs_loc)
4165 : {
4166 243615546 : if (std_attrs == error_mark_node)
4167 : return std_attrs;
4168 243615528 : tree attr = lookup_attribute ("gnu", "assume", std_attrs);
4169 243615528 : if (!attr)
4170 : return std_attrs;
4171 : /* The next token after the assume attribute is not ';'. */
4172 11686 : if (statement)
4173 : {
4174 12 : warning_at (attrs_loc, OPT_Wattributes,
4175 : "%<assume%> attribute not followed by %<;%>");
4176 12 : attr = NULL_TREE;
4177 : }
4178 23396 : for (; attr; attr = lookup_attribute ("gnu", "assume", TREE_CHAIN (attr)))
4179 : {
4180 11710 : tree args = TREE_VALUE (attr);
4181 11710 : if (args && PACK_EXPANSION_P (args))
4182 : {
4183 6 : auto_diagnostic_group d;
4184 6 : error_at (attrs_loc, "pack expansion of %qE attribute",
4185 : get_attribute_name (attr));
4186 6 : if (cxx_dialect >= cxx17)
4187 4 : inform (attrs_loc, "use fold expression in the attribute "
4188 : "argument instead");
4189 6 : continue;
4190 6 : }
4191 11704 : int nargs = list_length (args);
4192 11704 : if (nargs != 1)
4193 : {
4194 42 : auto_diagnostic_group d;
4195 42 : error_at (attrs_loc, "wrong number of arguments specified for "
4196 : "%qE attribute", get_attribute_name (attr));
4197 42 : inform (attrs_loc, "expected %i, found %i", 1, nargs);
4198 42 : }
4199 : else
4200 : {
4201 11662 : tree arg = TREE_VALUE (args);
4202 11662 : if (!type_dependent_expression_p (arg))
4203 11561 : arg = contextual_conv_bool (arg, tf_warning_or_error);
4204 11662 : if (error_operand_p (arg))
4205 18 : continue;
4206 11644 : finish_expr_stmt (build_assume_call (attrs_loc, arg));
4207 : }
4208 : }
4209 11686 : return remove_attribute ("gnu", "assume", std_attrs);
4210 : }
4211 :
4212 : /* Return the type std::source_location::__impl after performing
4213 : verification on it. */
4214 :
4215 : tree
4216 10315 : get_source_location_impl_type ()
4217 : {
4218 10315 : tree name = get_identifier ("source_location");
4219 10315 : tree decl = lookup_qualified_name (std_node, name);
4220 10315 : if (TREE_CODE (decl) != TYPE_DECL)
4221 : {
4222 6 : auto_diagnostic_group d;
4223 6 : if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
4224 3 : qualified_name_lookup_error (std_node, name, decl, input_location);
4225 : else
4226 3 : error ("%qD is not a type", decl);
4227 6 : return error_mark_node;
4228 6 : }
4229 10309 : name = get_identifier ("__impl");
4230 10309 : tree type = TREE_TYPE (decl);
4231 10309 : decl = lookup_qualified_name (type, name);
4232 10309 : if (TREE_CODE (decl) != TYPE_DECL)
4233 : {
4234 9 : auto_diagnostic_group d;
4235 9 : if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
4236 6 : qualified_name_lookup_error (type, name, decl, input_location);
4237 : else
4238 3 : error ("%qD is not a type", decl);
4239 9 : return error_mark_node;
4240 9 : }
4241 10300 : type = TREE_TYPE (decl);
4242 10300 : if (TREE_CODE (type) != RECORD_TYPE)
4243 : {
4244 3 : error ("%qD is not a class type", decl);
4245 3 : return error_mark_node;
4246 : }
4247 :
4248 10297 : int cnt = 0;
4249 10297 : for (tree field = TYPE_FIELDS (type);
4250 51446 : (field = next_aggregate_field (field)) != NULL_TREE;
4251 41149 : field = DECL_CHAIN (field))
4252 : {
4253 41158 : if (DECL_NAME (field) != NULL_TREE)
4254 : {
4255 41158 : const char *n = IDENTIFIER_POINTER (DECL_NAME (field));
4256 41158 : if (strcmp (n, "_M_file_name") == 0
4257 30864 : || strcmp (n, "_M_function_name") == 0)
4258 : {
4259 20585 : if (TREE_TYPE (field) != const_string_type_node)
4260 : {
4261 3 : error ("%qD does not have %<const char *%> type", field);
4262 3 : return error_mark_node;
4263 : }
4264 20582 : cnt++;
4265 20582 : continue;
4266 : }
4267 20573 : else if (strcmp (n, "_M_line") == 0 || strcmp (n, "_M_column") == 0)
4268 : {
4269 20570 : if (TREE_CODE (TREE_TYPE (field)) != INTEGER_TYPE)
4270 : {
4271 3 : error ("%qD does not have integral type", field);
4272 3 : return error_mark_node;
4273 : }
4274 20567 : cnt++;
4275 20567 : continue;
4276 : }
4277 : }
4278 : cnt = 0;
4279 : break;
4280 : }
4281 10291 : if (cnt != 4)
4282 : {
4283 9 : error ("%<std::source_location::__impl%> does not contain only "
4284 : "non-static data members %<_M_file_name%>, "
4285 : "%<_M_function_name%>, %<_M_line%> and %<_M_column%>");
4286 9 : return error_mark_node;
4287 : }
4288 10282 : return build_qualified_type (type, TYPE_QUAL_CONST);
4289 : }
4290 :
4291 : /* Type for source_location_table hash_set. */
4292 : struct GTY((for_user)) source_location_table_entry {
4293 : location_t loc;
4294 : unsigned uid;
4295 : tree var;
4296 : };
4297 :
4298 : /* Traits class for function start hash maps below. */
4299 :
4300 : struct source_location_table_entry_hash
4301 : : ggc_remove <source_location_table_entry>
4302 : {
4303 : typedef source_location_table_entry value_type;
4304 : typedef source_location_table_entry compare_type;
4305 :
4306 : static hashval_t
4307 57968 : hash (const source_location_table_entry &ref)
4308 : {
4309 57968 : inchash::hash hstate (0);
4310 57968 : hstate.add_int (ref.loc);
4311 57968 : hstate.add_int (ref.uid);
4312 57968 : return hstate.end ();
4313 : }
4314 :
4315 : static bool
4316 49827 : equal (const source_location_table_entry &ref1,
4317 : const source_location_table_entry &ref2)
4318 : {
4319 49827 : return ref1.loc == ref2.loc && ref1.uid == ref2.uid;
4320 : }
4321 :
4322 : static void
4323 : mark_deleted (source_location_table_entry &ref)
4324 : {
4325 : ref.loc = UNKNOWN_LOCATION;
4326 : ref.uid = -1U;
4327 : ref.var = NULL_TREE;
4328 : }
4329 :
4330 : static const bool empty_zero_p = true;
4331 :
4332 : static void
4333 0 : mark_empty (source_location_table_entry &ref)
4334 : {
4335 0 : ref.loc = UNKNOWN_LOCATION;
4336 0 : ref.uid = 0;
4337 0 : ref.var = NULL_TREE;
4338 : }
4339 :
4340 : static bool
4341 81757 : is_deleted (const source_location_table_entry &ref)
4342 : {
4343 81757 : return (ref.loc == UNKNOWN_LOCATION
4344 0 : && ref.uid == -1U
4345 81757 : && ref.var == NULL_TREE);
4346 : }
4347 :
4348 : static bool
4349 314187 : is_empty (const source_location_table_entry &ref)
4350 : {
4351 314187 : return (ref.loc == UNKNOWN_LOCATION
4352 141937 : && ref.uid == 0
4353 456124 : && ref.var == NULL_TREE);
4354 : }
4355 :
4356 : static void
4357 3 : pch_nx (source_location_table_entry &p)
4358 : {
4359 3 : extern void gt_pch_nx (source_location_table_entry &);
4360 3 : gt_pch_nx (p);
4361 : }
4362 :
4363 : static void
4364 3 : pch_nx (source_location_table_entry &p, gt_pointer_operator op, void *cookie)
4365 : {
4366 3 : extern void gt_pch_nx (source_location_table_entry *, gt_pointer_operator,
4367 : void *);
4368 3 : gt_pch_nx (&p, op, cookie);
4369 : }
4370 : };
4371 :
4372 : static GTY(()) hash_table <source_location_table_entry_hash>
4373 : *source_location_table;
4374 :
4375 : /* Build a std::source_location::__impl from a location_t. */
4376 :
4377 : tree
4378 12169 : build_source_location_impl (location_t loc, tree fndecl,
4379 : tree source_location_impl)
4380 : {
4381 12169 : if (source_location_table == NULL)
4382 395 : source_location_table
4383 395 : = hash_table <source_location_table_entry_hash>::create_ggc (64);
4384 12169 : const line_map_ordinary *map;
4385 12169 : source_location_table_entry entry;
4386 12169 : entry.loc
4387 12169 : = linemap_resolve_location (line_table, loc, LRK_MACRO_EXPANSION_POINT,
4388 : &map);
4389 12169 : entry.uid = fndecl ? DECL_UID (fndecl) : -1;
4390 12169 : entry.var = error_mark_node;
4391 12169 : source_location_table_entry *entryp
4392 12169 : = source_location_table->find_slot (entry, INSERT);
4393 :
4394 12169 : if (entryp->var)
4395 : return entryp->var;
4396 :
4397 8984 : tree var = build_decl (loc, VAR_DECL, generate_internal_label ("Lsrc_loc"),
4398 : source_location_impl);
4399 8984 : TREE_STATIC (var) = 1;
4400 8984 : TREE_PUBLIC (var) = 0;
4401 8984 : DECL_ARTIFICIAL (var) = 1;
4402 8984 : DECL_IGNORED_P (var) = 1;
4403 8984 : DECL_EXTERNAL (var) = 0;
4404 8984 : DECL_DECLARED_CONSTEXPR_P (var) = 1;
4405 8984 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var) = 1;
4406 8984 : layout_decl (var, 0);
4407 :
4408 8984 : vec<constructor_elt, va_gc> *v = NULL;
4409 8984 : vec_alloc (v, 4);
4410 8984 : for (tree field = TYPE_FIELDS (source_location_impl);
4411 44920 : (field = next_aggregate_field (field)) != NULL_TREE;
4412 35936 : field = DECL_CHAIN (field))
4413 : {
4414 35936 : const char *n = IDENTIFIER_POINTER (DECL_NAME (field));
4415 35936 : tree val = NULL_TREE;
4416 35936 : if (strcmp (n, "_M_file_name") == 0)
4417 : {
4418 8984 : if (const char *fname = LOCATION_FILE (loc))
4419 : {
4420 8984 : fname = remap_macro_filename (fname);
4421 8984 : val = build_string_literal (fname);
4422 : }
4423 : else
4424 0 : val = build_string_literal ("");
4425 : }
4426 26952 : else if (strcmp (n, "_M_function_name") == 0)
4427 : {
4428 8984 : const char *name = "";
4429 :
4430 8984 : if (fndecl)
4431 : {
4432 : /* If this is a coroutine, we should get the name of the user
4433 : function rather than the actor we generate. */
4434 8585 : if (tree ramp = DECL_RAMP_FN (fndecl))
4435 12 : name = cxx_printable_name (ramp, 2);
4436 : else
4437 8573 : name = cxx_printable_name (fndecl, 2);
4438 : }
4439 :
4440 8984 : val = build_string_literal (name);
4441 : }
4442 17968 : else if (strcmp (n, "_M_line") == 0)
4443 8984 : val = build_int_cst (TREE_TYPE (field), LOCATION_LINE (loc));
4444 8984 : else if (strcmp (n, "_M_column") == 0)
4445 8984 : val = build_int_cst (TREE_TYPE (field), LOCATION_COLUMN (loc));
4446 : else
4447 0 : gcc_unreachable ();
4448 35936 : CONSTRUCTOR_APPEND_ELT (v, field, val);
4449 : }
4450 :
4451 8984 : tree ctor = build_constructor (source_location_impl, v);
4452 8984 : TREE_CONSTANT (ctor) = 1;
4453 8984 : TREE_STATIC (ctor) = 1;
4454 8984 : DECL_INITIAL (var) = ctor;
4455 8984 : varpool_node::finalize_decl (var);
4456 8984 : *entryp = entry;
4457 8984 : entryp->var = var;
4458 8984 : return var;
4459 : }
4460 :
4461 : /* Fold the __builtin_source_location () call T. */
4462 :
4463 : tree
4464 11114 : fold_builtin_source_location (const_tree t)
4465 : {
4466 11114 : gcc_assert (TREE_CODE (t) == CALL_EXPR);
4467 : /* TREE_TYPE (t) is const std::source_location::__impl* */
4468 11114 : tree source_location_impl = TREE_TYPE (TREE_TYPE (t));
4469 11114 : if (source_location_impl == error_mark_node)
4470 0 : return build_zero_cst (const_ptr_type_node);
4471 11114 : gcc_assert (CLASS_TYPE_P (source_location_impl)
4472 : && id_equal (TYPE_IDENTIFIER (source_location_impl), "__impl"));
4473 :
4474 11114 : location_t loc = EXPR_LOCATION (t);
4475 11114 : tree var = build_source_location_impl (loc, current_function_decl,
4476 : source_location_impl);
4477 11114 : return build_fold_addr_expr_with_type_loc (loc, var, TREE_TYPE (t));
4478 : }
4479 :
4480 : #include "gt-cp-cp-gimplify.h"
|