Branch data Line data Source code
1 : : /* C++-specific tree lowering bits; see also c-gimplify.cc and gimple.cc.
2 : :
3 : : Copyright (C) 2002-2025 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 : :
47 : : /* Keep track of forward references to immediate-escalating functions in
48 : : case they become consteval. This vector contains ADDR_EXPRs and
49 : : PTRMEM_CSTs; it also stores FUNCTION_DECLs that had an escalating
50 : : function call in them, to check that they can be evaluated to a constant,
51 : : and immediate-escalating functions that may become consteval. */
52 : : static GTY(()) hash_set<tree> *deferred_escalating_exprs;
53 : :
54 : : static void
55 : 4518493 : remember_escalating_expr (tree t)
56 : : {
57 : 4518493 : if (uses_template_parms (t))
58 : : /* Templates don't escalate, and cp_fold_immediate can get confused by
59 : : other template trees in the function body (c++/115986). */
60 : : return;
61 : 4518493 : if (!deferred_escalating_exprs)
62 : 5619 : deferred_escalating_exprs = hash_set<tree>::create_ggc (37);
63 : 4518493 : deferred_escalating_exprs->add (t);
64 : : }
65 : :
66 : : /* Flags for cp_fold and cp_fold_r. */
67 : :
68 : : enum fold_flags {
69 : : ff_none = 0,
70 : : /* Whether we're being called from cp_fold_function. */
71 : : ff_genericize = 1 << 0,
72 : : /* Whether we're folding a point where we know we're
73 : : definitely not in a manifestly constant-evaluated
74 : : context. */
75 : : ff_mce_false = 1 << 1,
76 : : };
77 : :
78 : : using fold_flags_t = int;
79 : :
80 : 85025098 : struct cp_fold_data
81 : : {
82 : : hash_set<tree> pset;
83 : : fold_flags_t flags;
84 : 85025098 : cp_fold_data (fold_flags_t flags): flags (flags) {}
85 : : };
86 : :
87 : : /* Forward declarations. */
88 : :
89 : : static tree cp_genericize_r (tree *, int *, void *);
90 : : static tree cp_fold_r (tree *, int *, void *);
91 : : static void cp_genericize_tree (tree*, bool);
92 : : static tree cp_fold (tree, fold_flags_t);
93 : : static tree cp_fold_immediate_r (tree *, int *, void *);
94 : :
95 : : /* Genericize a TRY_BLOCK. */
96 : :
97 : : static void
98 : 16243 : genericize_try_block (tree *stmt_p)
99 : : {
100 : 16243 : tree body = TRY_STMTS (*stmt_p);
101 : 16243 : tree cleanup = TRY_HANDLERS (*stmt_p);
102 : :
103 : 16243 : *stmt_p = build2 (TRY_CATCH_EXPR, void_type_node, body, cleanup);
104 : 16243 : }
105 : :
106 : : /* Genericize a HANDLER by converting to a CATCH_EXPR. */
107 : :
108 : : static void
109 : 19021 : genericize_catch_block (tree *stmt_p)
110 : : {
111 : 19021 : tree type = HANDLER_TYPE (*stmt_p);
112 : 19021 : tree body = HANDLER_BODY (*stmt_p);
113 : :
114 : : /* FIXME should the caught type go in TREE_TYPE? */
115 : 19021 : *stmt_p = build2 (CATCH_EXPR, void_type_node, type, body);
116 : 19021 : }
117 : :
118 : : /* A terser interface for building a representation of an exception
119 : : specification. */
120 : :
121 : : static tree
122 : 4527 : build_gimple_eh_filter_tree (tree body, tree allowed, tree failure)
123 : : {
124 : 4527 : tree t;
125 : :
126 : : /* FIXME should the allowed types go in TREE_TYPE? */
127 : 4527 : t = build2 (EH_FILTER_EXPR, void_type_node, allowed, NULL_TREE);
128 : 4527 : append_to_statement_list (failure, &EH_FILTER_FAILURE (t));
129 : :
130 : 4527 : t = build2 (TRY_CATCH_EXPR, void_type_node, NULL_TREE, t);
131 : 4527 : append_to_statement_list (body, &TREE_OPERAND (t, 0));
132 : :
133 : 4527 : return t;
134 : : }
135 : :
136 : : /* Genericize an EH_SPEC_BLOCK by converting it to a
137 : : TRY_CATCH_EXPR/EH_FILTER_EXPR pair. */
138 : :
139 : : static void
140 : 4527 : genericize_eh_spec_block (tree *stmt_p)
141 : : {
142 : 4527 : tree body = EH_SPEC_STMTS (*stmt_p);
143 : 4527 : tree allowed = EH_SPEC_RAISES (*stmt_p);
144 : 4527 : tree failure = build_call_n (call_unexpected_fn, 1, build_exc_ptr ());
145 : :
146 : 4527 : *stmt_p = build_gimple_eh_filter_tree (body, allowed, failure);
147 : 4527 : suppress_warning (*stmt_p);
148 : 4527 : suppress_warning (TREE_OPERAND (*stmt_p, 1));
149 : 4527 : }
150 : :
151 : : /* Return the first non-compound statement in STMT. */
152 : :
153 : : tree
154 : 11424014 : first_stmt (tree stmt)
155 : : {
156 : 18193150 : switch (TREE_CODE (stmt))
157 : : {
158 : 4251739 : case STATEMENT_LIST:
159 : 4251739 : if (tree_statement_list_node *p = STATEMENT_LIST_HEAD (stmt))
160 : 3231444 : return first_stmt (p->stmt);
161 : 1020295 : return void_node;
162 : :
163 : 3537692 : case BIND_EXPR:
164 : 3537692 : return first_stmt (BIND_EXPR_BODY (stmt));
165 : :
166 : : default:
167 : : return stmt;
168 : : }
169 : : }
170 : :
171 : : /* Genericize an IF_STMT by turning it into a COND_EXPR. */
172 : :
173 : : static void
174 : 15790631 : genericize_if_stmt (tree *stmt_p)
175 : : {
176 : 15790631 : tree stmt, cond, then_, else_;
177 : 15790631 : location_t locus = EXPR_LOCATION (*stmt_p);
178 : :
179 : 15790631 : stmt = *stmt_p;
180 : 15790631 : cond = IF_COND (stmt);
181 : 15790631 : then_ = THEN_CLAUSE (stmt);
182 : 15790631 : else_ = ELSE_CLAUSE (stmt);
183 : :
184 : 15790631 : if (then_ && else_)
185 : : {
186 : 5712007 : tree ft = first_stmt (then_);
187 : 5712007 : tree fe = first_stmt (else_);
188 : 5712007 : br_predictor pr;
189 : 5712007 : if (TREE_CODE (ft) == PREDICT_EXPR
190 : 40787 : && TREE_CODE (fe) == PREDICT_EXPR
191 : 48 : && (pr = PREDICT_EXPR_PREDICTOR (ft)) == PREDICT_EXPR_PREDICTOR (fe)
192 : 5712046 : && (pr == PRED_HOT_LABEL || pr == PRED_COLD_LABEL))
193 : : {
194 : 3 : gcc_rich_location richloc (EXPR_LOC_OR_LOC (ft, locus));
195 : 3 : richloc.add_range (EXPR_LOC_OR_LOC (fe, locus));
196 : 3 : warning_at (&richloc, OPT_Wattributes,
197 : : "both branches of %<if%> statement marked as %qs",
198 : : pr == PRED_HOT_LABEL ? "likely" : "unlikely");
199 : 3 : }
200 : : }
201 : :
202 : 15790631 : if (!then_)
203 : 1897 : then_ = build_empty_stmt (locus);
204 : 15790631 : if (!else_)
205 : 10076767 : else_ = build_empty_stmt (locus);
206 : :
207 : : /* consteval if has been verified not to have the then_/else_ blocks
208 : : entered by gotos/case labels from elsewhere, and as then_ block
209 : : can contain unfolded immediate function calls, we have to discard
210 : : the then_ block regardless of whether else_ has side-effects or not. */
211 : 15790631 : if (IF_STMT_CONSTEVAL_P (stmt))
212 : : {
213 : 9869 : if (block_may_fallthru (then_))
214 : 2504 : stmt = build3 (COND_EXPR, void_type_node, boolean_false_node,
215 : : void_node, else_);
216 : : else
217 : : stmt = else_;
218 : : }
219 : 15780762 : else if (IF_STMT_CONSTEXPR_P (stmt))
220 : 2264010 : stmt = integer_nonzerop (cond) ? then_ : else_;
221 : : /* ??? This optimization doesn't seem to belong here, but removing it
222 : : causes -Wreturn-type regressions (e.g. 107310). */
223 : 13516752 : else if (integer_nonzerop (cond) && !TREE_SIDE_EFFECTS (else_))
224 : : stmt = then_;
225 : 13471587 : else if (integer_zerop (cond) && !TREE_SIDE_EFFECTS (then_))
226 : : stmt = else_;
227 : : else
228 : 13437080 : stmt = build3 (COND_EXPR, void_type_node, cond, then_, else_);
229 : 15790631 : protected_set_expr_location_if_unset (stmt, locus);
230 : 15790631 : *stmt_p = stmt;
231 : 15790631 : }
232 : :
233 : : /* Hook into the middle of gimplifying an OMP_FOR node. */
234 : :
235 : : static enum gimplify_status
236 : 45194 : cp_gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
237 : : {
238 : 45194 : tree for_stmt = *expr_p;
239 : 45194 : gimple_seq seq = NULL;
240 : :
241 : : /* Protect ourselves from recursion. */
242 : 45194 : if (OMP_FOR_GIMPLIFYING_P (for_stmt))
243 : : return GS_UNHANDLED;
244 : 21006 : OMP_FOR_GIMPLIFYING_P (for_stmt) = 1;
245 : :
246 : 21006 : gimplify_and_add (for_stmt, &seq);
247 : 21006 : gimple_seq_add_seq (pre_p, seq);
248 : :
249 : 21006 : OMP_FOR_GIMPLIFYING_P (for_stmt) = 0;
250 : :
251 : 21006 : return GS_ALL_DONE;
252 : : }
253 : :
254 : : /* Gimplify an EXPR_STMT node. */
255 : :
256 : : static void
257 : 3935728 : gimplify_expr_stmt (tree *stmt_p)
258 : : {
259 : 3935728 : tree stmt = EXPR_STMT_EXPR (*stmt_p);
260 : :
261 : 3935728 : if (stmt == error_mark_node)
262 : : stmt = NULL;
263 : :
264 : : /* Gimplification of a statement expression will nullify the
265 : : statement if all its side effects are moved to *PRE_P and *POST_P.
266 : :
267 : : In this case we will not want to emit the gimplified statement.
268 : : However, we may still want to emit a warning, so we do that before
269 : : gimplification. */
270 : 3931974 : if (stmt && warn_unused_value)
271 : : {
272 : 302662 : if (!TREE_SIDE_EFFECTS (stmt))
273 : : {
274 : 0 : if (!IS_EMPTY_STMT (stmt)
275 : 7138 : && !VOID_TYPE_P (TREE_TYPE (stmt))
276 : 7138 : && !warning_suppressed_p (stmt, OPT_Wunused_value))
277 : 0 : warning (OPT_Wunused_value, "statement with no effect");
278 : : }
279 : : else
280 : 295524 : warn_if_unused_value (stmt, input_location);
281 : : }
282 : :
283 : 3935728 : if (stmt == NULL_TREE)
284 : 3754 : stmt = alloc_stmt_list ();
285 : :
286 : 3935728 : *stmt_p = stmt;
287 : 3935728 : }
288 : :
289 : : /* Gimplify initialization from an AGGR_INIT_EXPR. */
290 : :
291 : : static void
292 : 10533043 : cp_gimplify_init_expr (tree *expr_p)
293 : : {
294 : 10533043 : tree from = TREE_OPERAND (*expr_p, 1);
295 : 10533043 : tree to = TREE_OPERAND (*expr_p, 0);
296 : 10533043 : tree t;
297 : :
298 : 10533043 : if (TREE_CODE (from) == TARGET_EXPR)
299 : 171160 : if (tree init = TARGET_EXPR_INITIAL (from))
300 : : {
301 : : /* Make sure that we expected to elide this temporary. But also allow
302 : : gimplify_modify_expr_rhs to elide temporaries of trivial type. */
303 : 171160 : gcc_checking_assert (TARGET_EXPR_ELIDING_P (from)
304 : : || !TREE_ADDRESSABLE (TREE_TYPE (from)));
305 : 171160 : if (target_expr_needs_replace (from))
306 : : {
307 : : /* If this was changed by cp_genericize_target_expr, we need to
308 : : walk into it to replace uses of the slot. */
309 : 31 : replace_decl (&init, TARGET_EXPR_SLOT (from), to);
310 : 31 : *expr_p = init;
311 : 31 : return;
312 : : }
313 : : else
314 : : from = init;
315 : : }
316 : :
317 : : /* Look through any COMPOUND_EXPRs, since build_compound_expr pushes them
318 : : inside the TARGET_EXPR. */
319 : 10594104 : for (t = from; t; )
320 : : {
321 : 10594104 : tree sub = TREE_CODE (t) == COMPOUND_EXPR ? TREE_OPERAND (t, 0) : t;
322 : :
323 : : /* If we are initializing from an AGGR_INIT_EXPR, drop the INIT_EXPR and
324 : : replace the slot operand with our target.
325 : :
326 : : Should we add a target parm to gimplify_expr instead? No, as in this
327 : : case we want to replace the INIT_EXPR. */
328 : 10594104 : if (TREE_CODE (sub) == AGGR_INIT_EXPR
329 : 10594104 : || TREE_CODE (sub) == VEC_INIT_EXPR)
330 : : {
331 : 99676 : if (TREE_CODE (sub) == AGGR_INIT_EXPR)
332 : 99676 : AGGR_INIT_EXPR_SLOT (sub) = to;
333 : : else
334 : 0 : VEC_INIT_EXPR_SLOT (sub) = to;
335 : 99676 : *expr_p = from;
336 : :
337 : : /* The initialization is now a side-effect, so the container can
338 : : become void. */
339 : 99676 : if (from != sub)
340 : 72 : TREE_TYPE (from) = void_type_node;
341 : : }
342 : :
343 : : /* Handle aggregate NSDMI. */
344 : 10594104 : replace_placeholders (sub, to);
345 : :
346 : 10594104 : if (t == sub)
347 : : break;
348 : : else
349 : 61092 : t = TREE_OPERAND (t, 1);
350 : : }
351 : :
352 : : }
353 : :
354 : : /* Gimplify a MUST_NOT_THROW_EXPR. */
355 : :
356 : : static enum gimplify_status
357 : 530877 : gimplify_must_not_throw_expr (tree *expr_p, gimple_seq *pre_p)
358 : : {
359 : 530877 : tree stmt = *expr_p;
360 : 530877 : tree temp = voidify_wrapper_expr (stmt, NULL);
361 : 530877 : tree body = TREE_OPERAND (stmt, 0);
362 : 530877 : gimple_seq try_ = NULL;
363 : 530877 : gimple_seq catch_ = NULL;
364 : 530877 : gimple *mnt;
365 : :
366 : 530877 : gimplify_and_add (body, &try_);
367 : 530877 : mnt = gimple_build_eh_must_not_throw (call_terminate_fn);
368 : 530877 : gimple_seq_add_stmt_without_update (&catch_, mnt);
369 : 530877 : mnt = gimple_build_try (try_, catch_, GIMPLE_TRY_CATCH);
370 : :
371 : 530877 : gimple_seq_add_stmt_without_update (pre_p, mnt);
372 : 530877 : if (temp)
373 : : {
374 : 33 : *expr_p = temp;
375 : 33 : return GS_OK;
376 : : }
377 : :
378 : 530844 : *expr_p = NULL;
379 : 530844 : return GS_ALL_DONE;
380 : : }
381 : :
382 : : /* Return TRUE if an operand (OP) of a given TYPE being copied is
383 : : really just an empty class copy.
384 : :
385 : : Check that the operand has a simple form so that TARGET_EXPRs and
386 : : non-empty CONSTRUCTORs get reduced properly, and we leave the
387 : : return slot optimization alone because it isn't a copy. */
388 : :
389 : : bool
390 : 17087809 : simple_empty_class_p (tree type, tree op, tree_code code)
391 : : {
392 : 19912604 : if (TREE_CODE (op) == COMPOUND_EXPR)
393 : 132301 : return simple_empty_class_p (type, TREE_OPERAND (op, 1), code);
394 : 3101815 : if (SIMPLE_TARGET_EXPR_P (op)
395 : 22477023 : && TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
396 : : /* The TARGET_EXPR is itself a simple copy, look through it. */
397 : 2692494 : return simple_empty_class_p (type, TARGET_EXPR_INITIAL (op), code);
398 : :
399 : 17087809 : if (TREE_CODE (op) == PARM_DECL
400 : 17087809 : && TREE_ADDRESSABLE (TREE_TYPE (op)))
401 : : {
402 : 4 : tree fn = DECL_CONTEXT (op);
403 : 4 : if (DECL_THUNK_P (fn)
404 : 8 : || lambda_static_thunk_p (fn))
405 : : /* In a thunk, we pass through invisible reference parms, so this isn't
406 : : actually a copy. */
407 : 3 : return false;
408 : : }
409 : :
410 : 17087806 : return
411 : 17087806 : (TREE_CODE (op) == EMPTY_CLASS_EXPR
412 : 17087776 : || code == MODIFY_EXPR
413 : 14264380 : || is_gimple_lvalue (op)
414 : 11094867 : || INDIRECT_REF_P (op)
415 : 10750333 : || (TREE_CODE (op) == CONSTRUCTOR
416 : 2287831 : && CONSTRUCTOR_NELTS (op) == 0)
417 : 8637182 : || (TREE_CODE (op) == CALL_EXPR
418 : 2379507 : && !CALL_EXPR_RETURN_SLOT_OPT (op)))
419 : 10750041 : && !TREE_CLOBBER_P (op)
420 : 27187265 : && is_really_empty_class (type, /*ignore_vptr*/true);
421 : : }
422 : :
423 : : /* Returns true if evaluating E as an lvalue has side-effects;
424 : : specifically, a volatile lvalue has TREE_SIDE_EFFECTS, but it doesn't really
425 : : have side-effects until there is a read or write through it. */
426 : :
427 : : static bool
428 : 2482432 : lvalue_has_side_effects (tree e)
429 : : {
430 : 2482432 : if (!TREE_SIDE_EFFECTS (e))
431 : : return false;
432 : 54439 : while (handled_component_p (e))
433 : : {
434 : 3022 : if (TREE_CODE (e) == ARRAY_REF
435 : 3022 : && TREE_SIDE_EFFECTS (TREE_OPERAND (e, 1)))
436 : : return true;
437 : 1629 : e = TREE_OPERAND (e, 0);
438 : : }
439 : 51417 : if (DECL_P (e))
440 : : /* Just naming a variable has no side-effects. */
441 : : return false;
442 : 35266 : else if (INDIRECT_REF_P (e))
443 : : /* Similarly, indirection has no side-effects. */
444 : 35172 : return TREE_SIDE_EFFECTS (TREE_OPERAND (e, 0));
445 : : else
446 : : /* For anything else, trust TREE_SIDE_EFFECTS. */
447 : 94 : return TREE_SIDE_EFFECTS (e);
448 : : }
449 : :
450 : : /* Return true if FN is an immediate-escalating function. */
451 : :
452 : : static bool
453 : 51241579 : immediate_escalating_function_p (tree fn)
454 : : {
455 : 51241579 : if (!fn || !flag_immediate_escalation)
456 : : return false;
457 : :
458 : 51162137 : gcc_checking_assert (TREE_CODE (fn) == FUNCTION_DECL);
459 : :
460 : 51162137 : if (DECL_IMMEDIATE_FUNCTION_P (fn))
461 : : return false;
462 : :
463 : : /* An immediate-escalating function is
464 : : -- the call operator of a lambda that is not declared with the consteval
465 : : specifier */
466 : 52833008 : if (LAMBDA_FUNCTION_P (fn))
467 : : return true;
468 : : /* -- a defaulted special member function that is not declared with the
469 : : consteval specifier */
470 : 49993519 : special_function_kind sfk = special_memfn_p (fn);
471 : 49993519 : if (sfk != sfk_none && DECL_DEFAULTED_FN (fn))
472 : : return true;
473 : : /* -- a function that results from the instantiation of a templated entity
474 : : defined with the constexpr specifier. */
475 : 49476126 : return is_instantiation_of_constexpr (fn);
476 : : }
477 : :
478 : : /* Return true if FN is an immediate-escalating function that has not been
479 : : checked for escalating expressions.. */
480 : :
481 : : static bool
482 : 51240977 : unchecked_immediate_escalating_function_p (tree fn)
483 : : {
484 : 51240977 : return (immediate_escalating_function_p (fn)
485 : 51240977 : && !DECL_ESCALATION_CHECKED_P (fn));
486 : : }
487 : :
488 : : /* Promote FN to an immediate function, including its clones. */
489 : :
490 : : static void
491 : 183 : promote_function_to_consteval (tree fn)
492 : : {
493 : 183 : SET_DECL_IMMEDIATE_FUNCTION_P (fn);
494 : 183 : DECL_ESCALATION_CHECKED_P (fn) = true;
495 : 183 : tree clone;
496 : 219 : FOR_EACH_CLONE (clone, fn)
497 : : {
498 : 36 : SET_DECL_IMMEDIATE_FUNCTION_P (clone);
499 : 36 : DECL_ESCALATION_CHECKED_P (clone) = true;
500 : : }
501 : 183 : }
502 : :
503 : : /* A wrapper around cp_fold_immediate_r. Return a non-null tree if
504 : : we found a non-constant immediate function, or taking the address
505 : : of an immediate function. */
506 : :
507 : : tree
508 : 3598099 : cp_fold_immediate (tree *tp, mce_value manifestly_const_eval,
509 : : tree decl /*= current_function_decl*/)
510 : : {
511 : 3598099 : if (cxx_dialect <= cxx17)
512 : : return NULL_TREE;
513 : :
514 : 3172991 : temp_override<tree> cfd (current_function_decl, decl);
515 : :
516 : 3172991 : fold_flags_t flags = ff_none;
517 : 3172991 : if (manifestly_const_eval == mce_false)
518 : 1834436 : flags |= ff_mce_false;
519 : :
520 : 3172991 : cp_fold_data data (flags);
521 : 3172991 : int save_errorcount = errorcount;
522 : 3172991 : tree r = cp_walk_tree_without_duplicates (tp, cp_fold_immediate_r, &data);
523 : 3172991 : if (errorcount > save_errorcount)
524 : 49 : return integer_one_node;
525 : : return r;
526 : 3172991 : }
527 : :
528 : : /* Maybe say that FN (a function decl with DECL_IMMEDIATE_FUNCTION_P set)
529 : : was initially not an immediate function, but was promoted to one because
530 : : its body contained an immediate-escalating expression or conversion. */
531 : :
532 : : static void
533 : 329 : maybe_explain_promoted_consteval (location_t loc, tree fn)
534 : : {
535 : 329 : if (DECL_ESCALATION_CHECKED_P (fn))
536 : : {
537 : : /* See if we can figure out what made the function consteval. */
538 : 126 : tree x = cp_fold_immediate (&DECL_SAVED_TREE (fn), mce_unknown, NULL_TREE);
539 : 126 : if (x)
540 : 102 : inform (cp_expr_loc_or_loc (x, loc),
541 : : "%qD was promoted to an immediate function because its "
542 : : "body contains an immediate-escalating expression %qE", fn, x);
543 : : else
544 : 24 : inform (loc, "%qD was promoted to an immediate function", fn);
545 : : }
546 : 329 : }
547 : :
548 : : /* Gimplify *EXPR_P as rvalue into an expression that can't be modified
549 : : by expressions with side-effects in other operands. */
550 : :
551 : : static enum gimplify_status
552 : 34452 : gimplify_to_rvalue (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
553 : : bool (*gimple_test_f) (tree))
554 : : {
555 : 34452 : enum gimplify_status t
556 : 34452 : = gimplify_expr (expr_p, pre_p, post_p, gimple_test_f, fb_rvalue);
557 : 34452 : if (t == GS_ERROR)
558 : : return GS_ERROR;
559 : 34449 : else if (is_gimple_variable (*expr_p) && TREE_CODE (*expr_p) != SSA_NAME)
560 : 2857 : *expr_p = get_initialized_tmp_var (*expr_p, pre_p);
561 : : return t;
562 : : }
563 : :
564 : : /* Like gimplify_arg, but if ORDERED is set (which should be set if
565 : : any of the arguments this argument is sequenced before has
566 : : TREE_SIDE_EFFECTS set, make sure expressions with is_gimple_reg_type type
567 : : are gimplified into SSA_NAME or a fresh temporary and for
568 : : non-is_gimple_reg_type we don't optimize away TARGET_EXPRs. */
569 : :
570 : : static enum gimplify_status
571 : 3641505 : cp_gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location,
572 : : bool ordered)
573 : : {
574 : 3641505 : enum gimplify_status t;
575 : 3641505 : if (ordered
576 : 356035 : && !is_gimple_reg_type (TREE_TYPE (*arg_p))
577 : 3643262 : && TREE_CODE (*arg_p) == TARGET_EXPR)
578 : : {
579 : : /* gimplify_arg would strip away the TARGET_EXPR, but
580 : : that can mean we don't copy the argument and some following
581 : : argument with side-effect could modify it. */
582 : 1609 : protected_set_expr_location (*arg_p, call_location);
583 : 1609 : return gimplify_expr (arg_p, pre_p, NULL, is_gimple_lvalue, fb_either);
584 : : }
585 : : else
586 : : {
587 : 3639896 : t = gimplify_arg (arg_p, pre_p, call_location);
588 : 3639896 : if (t == GS_ERROR)
589 : : return GS_ERROR;
590 : 3639896 : else if (ordered
591 : 354426 : && is_gimple_reg_type (TREE_TYPE (*arg_p))
592 : 354278 : && is_gimple_variable (*arg_p)
593 : 199957 : && TREE_CODE (*arg_p) != SSA_NAME
594 : : /* No need to force references into register, references
595 : : can't be modified. */
596 : 121869 : && !TYPE_REF_P (TREE_TYPE (*arg_p))
597 : : /* And this can't be modified either. */
598 : 3730783 : && *arg_p != current_class_ptr)
599 : 9577 : *arg_p = get_initialized_tmp_var (*arg_p, pre_p);
600 : 3639896 : return t;
601 : : }
602 : :
603 : : }
604 : :
605 : : /* Do C++-specific gimplification. Args are as for gimplify_expr. */
606 : :
607 : : int
608 : 157016169 : cp_gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
609 : : {
610 : 157016169 : int saved_stmts_are_full_exprs_p = 0;
611 : 157016169 : location_t loc = cp_expr_loc_or_input_loc (*expr_p);
612 : 157016169 : enum tree_code code = TREE_CODE (*expr_p);
613 : 157016169 : enum gimplify_status ret;
614 : :
615 : 157016169 : if (STATEMENT_CODE_P (code))
616 : : {
617 : 3975519 : saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
618 : 7951038 : current_stmt_tree ()->stmts_are_full_exprs_p
619 : 3975519 : = STMT_IS_FULL_EXPR_P (*expr_p);
620 : : }
621 : :
622 : 157016169 : switch (code)
623 : : {
624 : 317206 : case AGGR_INIT_EXPR:
625 : 317206 : simplify_aggr_init_expr (expr_p);
626 : 317206 : ret = GS_OK;
627 : 317206 : break;
628 : :
629 : 0 : case VEC_INIT_EXPR:
630 : 0 : {
631 : 0 : *expr_p = expand_vec_init_expr (NULL_TREE, *expr_p,
632 : : tf_warning_or_error);
633 : :
634 : 0 : cp_fold_data data (ff_genericize | ff_mce_false);
635 : 0 : cp_walk_tree (expr_p, cp_fold_r, &data, NULL);
636 : 0 : cp_genericize_tree (expr_p, false);
637 : 0 : copy_if_shared (expr_p);
638 : 0 : ret = GS_OK;
639 : 0 : }
640 : 0 : break;
641 : :
642 : 17380 : case THROW_EXPR:
643 : : /* FIXME communicate throw type to back end, probably by moving
644 : : THROW_EXPR into ../tree.def. */
645 : 17380 : *expr_p = TREE_OPERAND (*expr_p, 0);
646 : 17380 : ret = GS_OK;
647 : 17380 : break;
648 : :
649 : 530877 : case MUST_NOT_THROW_EXPR:
650 : 530877 : ret = gimplify_must_not_throw_expr (expr_p, pre_p);
651 : 530877 : break;
652 : :
653 : : /* We used to do this for MODIFY_EXPR as well, but that's unsafe; the
654 : : LHS of an assignment might also be involved in the RHS, as in bug
655 : : 25979. */
656 : 10533043 : case INIT_EXPR:
657 : 10533043 : cp_gimplify_init_expr (expr_p);
658 : 10533043 : if (TREE_CODE (*expr_p) != INIT_EXPR)
659 : : return GS_OK;
660 : : /* Fall through. */
661 : 14247957 : case MODIFY_EXPR:
662 : 10433336 : modify_expr_case:
663 : 14247957 : {
664 : : /* If the back end isn't clever enough to know that the lhs and rhs
665 : : types are the same, add an explicit conversion. */
666 : 14247957 : tree op0 = TREE_OPERAND (*expr_p, 0);
667 : 14247957 : tree op1 = TREE_OPERAND (*expr_p, 1);
668 : :
669 : 14247957 : if (!error_operand_p (op0)
670 : 14247957 : && !error_operand_p (op1)
671 : 14247917 : && (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (op0))
672 : 14245272 : || TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (op1)))
673 : 14250611 : && !useless_type_conversion_p (TREE_TYPE (op1), TREE_TYPE (op0)))
674 : 9 : TREE_OPERAND (*expr_p, 1) = build1 (VIEW_CONVERT_EXPR,
675 : 9 : TREE_TYPE (op0), op1);
676 : :
677 : 14247948 : else if (simple_empty_class_p (TREE_TYPE (op0), op1, code))
678 : : {
679 : 137314 : while (TREE_CODE (op1) == TARGET_EXPR)
680 : : /* We're disconnecting the initializer from its target,
681 : : don't create a temporary. */
682 : 7141 : op1 = TARGET_EXPR_INITIAL (op1);
683 : :
684 : : /* Remove any copies of empty classes. Also drop volatile
685 : : variables on the RHS to avoid infinite recursion from
686 : : gimplify_expr trying to load the value. */
687 : 130173 : if (TREE_SIDE_EFFECTS (op1))
688 : : {
689 : 11221 : if (TREE_THIS_VOLATILE (op1)
690 : 0 : && (REFERENCE_CLASS_P (op1) || DECL_P (op1)))
691 : 0 : op1 = build_fold_addr_expr (op1);
692 : :
693 : 11221 : gimplify_and_add (op1, pre_p);
694 : : }
695 : 130173 : gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
696 : : is_gimple_lvalue, fb_lvalue);
697 : 130173 : *expr_p = TREE_OPERAND (*expr_p, 0);
698 : 130173 : if (code == RETURN_EXPR && REFERENCE_CLASS_P (*expr_p))
699 : : /* Avoid 'return *<retval>;' */
700 : 9 : *expr_p = TREE_OPERAND (*expr_p, 0);
701 : : }
702 : : /* P0145 says that the RHS is sequenced before the LHS.
703 : : gimplify_modify_expr gimplifies the RHS before the LHS, but that
704 : : isn't quite strong enough in two cases:
705 : :
706 : : 1) gimplify.cc wants to leave a CALL_EXPR on the RHS, which would
707 : : mean it's evaluated after the LHS.
708 : :
709 : : 2) the value calculation of the RHS is also sequenced before the
710 : : LHS, so for scalar assignment we need to preevaluate if the
711 : : RHS could be affected by LHS side-effects even if it has no
712 : : side-effects of its own. We don't need this for classes because
713 : : class assignment takes its RHS by reference. */
714 : 14117775 : else if (flag_strong_eval_order > 1
715 : 12803747 : && TREE_CODE (*expr_p) == MODIFY_EXPR
716 : 2482432 : && lvalue_has_side_effects (op0)
717 : 14154296 : && (TREE_CODE (op1) == CALL_EXPR
718 : 29626 : || (SCALAR_TYPE_P (TREE_TYPE (op1))
719 : 24576 : && !TREE_CONSTANT (op1))))
720 : 21016 : TREE_OPERAND (*expr_p, 1) = get_initialized_tmp_var (op1, pre_p);
721 : : }
722 : : ret = GS_OK;
723 : : break;
724 : :
725 : 65243 : case EMPTY_CLASS_EXPR:
726 : : /* We create an empty CONSTRUCTOR with RECORD_TYPE. */
727 : 65243 : *expr_p = build_constructor (TREE_TYPE (*expr_p), NULL);
728 : 65243 : ret = GS_OK;
729 : 65243 : break;
730 : :
731 : 0 : case BASELINK:
732 : 0 : *expr_p = BASELINK_FUNCTIONS (*expr_p);
733 : 0 : ret = GS_OK;
734 : 0 : break;
735 : :
736 : 16243 : case TRY_BLOCK:
737 : 16243 : genericize_try_block (expr_p);
738 : 16243 : ret = GS_OK;
739 : 16243 : break;
740 : :
741 : 19021 : case HANDLER:
742 : 19021 : genericize_catch_block (expr_p);
743 : 19021 : ret = GS_OK;
744 : 19021 : break;
745 : :
746 : 4527 : case EH_SPEC_BLOCK:
747 : 4527 : genericize_eh_spec_block (expr_p);
748 : 4527 : ret = GS_OK;
749 : 4527 : break;
750 : :
751 : 0 : case USING_STMT:
752 : 0 : gcc_unreachable ();
753 : :
754 : 0 : case FOR_STMT:
755 : 0 : case WHILE_STMT:
756 : 0 : case DO_STMT:
757 : 0 : case SWITCH_STMT:
758 : 0 : case CONTINUE_STMT:
759 : 0 : case BREAK_STMT:
760 : 0 : gcc_unreachable ();
761 : :
762 : 45194 : case OMP_FOR:
763 : 45194 : case OMP_SIMD:
764 : 45194 : case OMP_DISTRIBUTE:
765 : 45194 : case OMP_LOOP:
766 : 45194 : case OMP_TASKLOOP:
767 : 45194 : case OMP_TILE:
768 : 45194 : case OMP_UNROLL:
769 : 45194 : ret = cp_gimplify_omp_for (expr_p, pre_p);
770 : 45194 : break;
771 : :
772 : 3935728 : case EXPR_STMT:
773 : 3935728 : gimplify_expr_stmt (expr_p);
774 : 3935728 : ret = GS_OK;
775 : 3935728 : break;
776 : :
777 : 0 : case UNARY_PLUS_EXPR:
778 : 0 : {
779 : 0 : tree arg = TREE_OPERAND (*expr_p, 0);
780 : 0 : tree type = TREE_TYPE (*expr_p);
781 : 0 : *expr_p = (TREE_TYPE (arg) != type) ? fold_convert (type, arg)
782 : : : arg;
783 : 0 : ret = GS_OK;
784 : : }
785 : 0 : break;
786 : :
787 : 7364317 : case CALL_EXPR:
788 : 7364317 : ret = GS_OK;
789 : 7364317 : if (flag_strong_eval_order == 2
790 : 6900509 : && CALL_EXPR_FN (*expr_p)
791 : 6714480 : && !CALL_EXPR_OPERATOR_SYNTAX (*expr_p)
792 : 13458903 : && cp_get_callee_fndecl_nofold (*expr_p) == NULL_TREE)
793 : : {
794 : 34452 : tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
795 : 34452 : enum gimplify_status t
796 : 34452 : = gimplify_to_rvalue (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
797 : : is_gimple_call_addr);
798 : 34452 : if (t == GS_ERROR)
799 : : ret = GS_ERROR;
800 : : /* GIMPLE considers most pointer conversion useless, but for
801 : : calls we actually care about the exact function pointer type. */
802 : 34449 : else if (TREE_TYPE (CALL_EXPR_FN (*expr_p)) != fnptrtype)
803 : 8456 : CALL_EXPR_FN (*expr_p)
804 : 16912 : = build1 (NOP_EXPR, fnptrtype, CALL_EXPR_FN (*expr_p));
805 : : }
806 : 7364317 : if (!CALL_EXPR_FN (*expr_p))
807 : : /* Internal function call. */;
808 : 7155800 : else if (CALL_EXPR_REVERSE_ARGS (*expr_p))
809 : : {
810 : : /* This is a call to a (compound) assignment operator that used
811 : : the operator syntax; gimplify the RHS first. */
812 : 45100 : gcc_assert (call_expr_nargs (*expr_p) == 2);
813 : 45100 : gcc_assert (!CALL_EXPR_ORDERED_ARGS (*expr_p));
814 : 45100 : enum gimplify_status t
815 : 45100 : = cp_gimplify_arg (&CALL_EXPR_ARG (*expr_p, 1), pre_p, loc,
816 : 45100 : TREE_SIDE_EFFECTS (CALL_EXPR_ARG (*expr_p, 0)));
817 : 45100 : if (t == GS_ERROR)
818 : : ret = GS_ERROR;
819 : : }
820 : 7110700 : else if (CALL_EXPR_ORDERED_ARGS (*expr_p))
821 : : {
822 : : /* Leave the last argument for gimplify_call_expr, to avoid problems
823 : : with __builtin_va_arg_pack(). */
824 : 164383 : int nargs = call_expr_nargs (*expr_p) - 1;
825 : 164383 : int last_side_effects_arg = -1;
826 : 322417 : for (int i = nargs; i > 0; --i)
827 : 176253 : if (TREE_SIDE_EFFECTS (CALL_EXPR_ARG (*expr_p, i)))
828 : : {
829 : : last_side_effects_arg = i;
830 : : break;
831 : : }
832 : 349126 : for (int i = 0; i < nargs; ++i)
833 : : {
834 : 184743 : enum gimplify_status t
835 : 184743 : = cp_gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p, loc,
836 : : i < last_side_effects_arg);
837 : 184743 : if (t == GS_ERROR)
838 : 0 : ret = GS_ERROR;
839 : : }
840 : : }
841 : 6946317 : else if (flag_strong_eval_order
842 : 6946317 : && !CALL_EXPR_OPERATOR_SYNTAX (*expr_p))
843 : : {
844 : : /* If flag_strong_eval_order, evaluate the object argument first. */
845 : 6482974 : tree fntype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
846 : 6482974 : if (INDIRECT_TYPE_P (fntype))
847 : 6482971 : fntype = TREE_TYPE (fntype);
848 : 6482974 : if (TREE_CODE (fntype) == METHOD_TYPE)
849 : : {
850 : 3411662 : int nargs = call_expr_nargs (*expr_p);
851 : 3411662 : bool side_effects = false;
852 : 4710945 : for (int i = 1; i < nargs; ++i)
853 : 1622918 : if (TREE_SIDE_EFFECTS (CALL_EXPR_ARG (*expr_p, i)))
854 : : {
855 : : side_effects = true;
856 : : break;
857 : : }
858 : 3411662 : enum gimplify_status t
859 : 3411662 : = cp_gimplify_arg (&CALL_EXPR_ARG (*expr_p, 0), pre_p, loc,
860 : : side_effects);
861 : 3411662 : if (t == GS_ERROR)
862 : : ret = GS_ERROR;
863 : : }
864 : : }
865 : 7364317 : if (ret != GS_ERROR)
866 : : {
867 : 7364314 : tree decl = cp_get_callee_fndecl_nofold (*expr_p);
868 : 7364314 : if (!decl)
869 : : break;
870 : 7116515 : if (fndecl_built_in_p (decl, BUILT_IN_FRONTEND))
871 : 0 : switch (DECL_FE_FUNCTION_CODE (decl))
872 : : {
873 : 0 : case CP_BUILT_IN_IS_CONSTANT_EVALUATED:
874 : 0 : *expr_p = boolean_false_node;
875 : 0 : break;
876 : 0 : case CP_BUILT_IN_SOURCE_LOCATION:
877 : 0 : *expr_p
878 : 0 : = fold_builtin_source_location (*expr_p);
879 : 0 : break;
880 : 0 : case CP_BUILT_IN_IS_CORRESPONDING_MEMBER:
881 : 0 : *expr_p
882 : 0 : = fold_builtin_is_corresponding_member
883 : 0 : (EXPR_LOCATION (*expr_p), call_expr_nargs (*expr_p),
884 : : &CALL_EXPR_ARG (*expr_p, 0));
885 : 0 : break;
886 : 0 : case CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS:
887 : 0 : *expr_p
888 : 0 : = fold_builtin_is_pointer_inverconvertible_with_class
889 : 0 : (EXPR_LOCATION (*expr_p), call_expr_nargs (*expr_p),
890 : : &CALL_EXPR_ARG (*expr_p, 0));
891 : 0 : break;
892 : : default:
893 : : break;
894 : : }
895 : 7116515 : else if (fndecl_built_in_p (decl, BUILT_IN_CLZG, BUILT_IN_CTZG))
896 : 27 : ret = (enum gimplify_status) c_gimplify_expr (expr_p, pre_p,
897 : : post_p);
898 : : else
899 : : /* All consteval functions should have been processed by now. */
900 : 7116488 : gcc_checking_assert (!immediate_invocation_p (decl));
901 : : }
902 : : break;
903 : :
904 : 528430 : case TARGET_EXPR:
905 : : /* A TARGET_EXPR that expresses direct-initialization should have been
906 : : elided by cp_gimplify_init_expr. */
907 : 528430 : gcc_checking_assert (!TARGET_EXPR_DIRECT_INIT_P (*expr_p));
908 : : /* Likewise, but allow extra temps of trivial type so that
909 : : gimplify_init_ctor_preeval can materialize subobjects of a CONSTRUCTOR
910 : : on the rhs of an assignment, as in constexpr-aggr1.C. */
911 : 528430 : gcc_checking_assert (!TARGET_EXPR_ELIDING_P (*expr_p)
912 : : || !TREE_ADDRESSABLE (TREE_TYPE (*expr_p)));
913 : : ret = GS_UNHANDLED;
914 : : break;
915 : :
916 : 3 : case PTRMEM_CST:
917 : 3 : *expr_p = cplus_expand_constant (*expr_p);
918 : 3 : if (TREE_CODE (*expr_p) == PTRMEM_CST)
919 : : ret = GS_ERROR;
920 : : else
921 : 25987595 : ret = GS_OK;
922 : : break;
923 : :
924 : 1048001 : case RETURN_EXPR:
925 : 1048001 : if (TREE_OPERAND (*expr_p, 0)
926 : 1048001 : && (TREE_CODE (TREE_OPERAND (*expr_p, 0)) == INIT_EXPR
927 : 12546 : || TREE_CODE (TREE_OPERAND (*expr_p, 0)) == MODIFY_EXPR))
928 : : {
929 : 991225 : expr_p = &TREE_OPERAND (*expr_p, 0);
930 : : /* Avoid going through the INIT_EXPR case, which can
931 : : degrade INIT_EXPRs into AGGR_INIT_EXPRs. */
932 : 991225 : goto modify_expr_case;
933 : : }
934 : : /* Fall through. */
935 : :
936 : 129824336 : default:
937 : 129824336 : ret = (enum gimplify_status) c_gimplify_expr (expr_p, pre_p, post_p);
938 : 129824336 : break;
939 : : }
940 : :
941 : : /* Restore saved state. */
942 : 156916462 : if (STATEMENT_CODE_P (code))
943 : 3975519 : current_stmt_tree ()->stmts_are_full_exprs_p
944 : 3975519 : = saved_stmts_are_full_exprs_p;
945 : :
946 : : return ret;
947 : : }
948 : :
949 : : static inline bool
950 : 1561929550 : is_invisiref_parm (const_tree t)
951 : : {
952 : 1465215585 : return ((TREE_CODE (t) == PARM_DECL || TREE_CODE (t) == RESULT_DECL)
953 : 1595703458 : && DECL_BY_REFERENCE (t));
954 : : }
955 : :
956 : : /* A stable comparison routine for use with splay trees and DECLs. */
957 : :
958 : : static int
959 : 61854 : splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
960 : : {
961 : 61854 : tree a = (tree) xa;
962 : 61854 : tree b = (tree) xb;
963 : :
964 : 61854 : return DECL_UID (a) - DECL_UID (b);
965 : : }
966 : :
967 : : /* OpenMP context during genericization. */
968 : :
969 : : struct cp_genericize_omp_taskreg
970 : : {
971 : : bool is_parallel;
972 : : bool default_shared;
973 : : struct cp_genericize_omp_taskreg *outer;
974 : : splay_tree variables;
975 : : };
976 : :
977 : : /* Return true if genericization should try to determine if
978 : : DECL is firstprivate or shared within task regions. */
979 : :
980 : : static bool
981 : 118396 : omp_var_to_track (tree decl)
982 : : {
983 : 118396 : tree type = TREE_TYPE (decl);
984 : 118396 : if (is_invisiref_parm (decl))
985 : 537 : type = TREE_TYPE (type);
986 : 117859 : else if (TYPE_REF_P (type))
987 : 5451 : type = TREE_TYPE (type);
988 : 143357 : while (TREE_CODE (type) == ARRAY_TYPE)
989 : 24961 : type = TREE_TYPE (type);
990 : 118396 : if (type == error_mark_node || !CLASS_TYPE_P (type))
991 : : return false;
992 : 13837 : if (VAR_P (decl) && CP_DECL_THREAD_LOCAL_P (decl))
993 : : return false;
994 : 13834 : if (cxx_omp_predetermined_sharing (decl) != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
995 : : return false;
996 : : return true;
997 : : }
998 : :
999 : : /* Note DECL use in OpenMP region OMP_CTX during genericization. */
1000 : :
1001 : : static void
1002 : 14030 : omp_cxx_notice_variable (struct cp_genericize_omp_taskreg *omp_ctx, tree decl)
1003 : : {
1004 : 14030 : splay_tree_node n = splay_tree_lookup (omp_ctx->variables,
1005 : : (splay_tree_key) decl);
1006 : 14030 : if (n == NULL)
1007 : : {
1008 : 4284 : int flags = OMP_CLAUSE_DEFAULT_SHARED;
1009 : 4284 : if (omp_ctx->outer)
1010 : 1257 : omp_cxx_notice_variable (omp_ctx->outer, decl);
1011 : 4284 : if (!omp_ctx->default_shared)
1012 : : {
1013 : 948 : struct cp_genericize_omp_taskreg *octx;
1014 : :
1015 : 1065 : for (octx = omp_ctx->outer; octx; octx = octx->outer)
1016 : : {
1017 : 902 : n = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
1018 : 902 : if (n && n->value != OMP_CLAUSE_DEFAULT_SHARED)
1019 : : {
1020 : : flags = OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
1021 : : break;
1022 : : }
1023 : 859 : if (octx->is_parallel)
1024 : : break;
1025 : : }
1026 : 948 : if (octx == NULL
1027 : 948 : && (TREE_CODE (decl) == PARM_DECL
1028 : 120 : || (!(TREE_STATIC (decl) || DECL_EXTERNAL (decl))
1029 : 44 : && DECL_CONTEXT (decl) == current_function_decl)))
1030 : : flags = OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
1031 : 861 : if (flags == OMP_CLAUSE_DEFAULT_FIRSTPRIVATE)
1032 : : {
1033 : : /* DECL is implicitly determined firstprivate in
1034 : : the current task construct. Ensure copy ctor and
1035 : : dtor are instantiated, because during gimplification
1036 : : it will be already too late. */
1037 : 130 : tree type = TREE_TYPE (decl);
1038 : 130 : if (is_invisiref_parm (decl))
1039 : 2 : type = TREE_TYPE (type);
1040 : 128 : else if (TYPE_REF_P (type))
1041 : 55 : type = TREE_TYPE (type);
1042 : 180 : while (TREE_CODE (type) == ARRAY_TYPE)
1043 : 50 : type = TREE_TYPE (type);
1044 : 130 : get_copy_ctor (type, tf_none);
1045 : 130 : get_dtor (type, tf_none);
1046 : : }
1047 : : }
1048 : 4284 : splay_tree_insert (omp_ctx->variables, (splay_tree_key) decl, flags);
1049 : : }
1050 : 14030 : }
1051 : :
1052 : : /* True if any of the element initializers in CTOR are TARGET_EXPRs that are
1053 : : not expected to elide, e.g. because unsafe_copy_elision_p is true. */
1054 : :
1055 : : static bool
1056 : 47884 : any_non_eliding_target_exprs (tree ctor)
1057 : : {
1058 : 122000 : for (const constructor_elt &e : *CONSTRUCTOR_ELTS (ctor))
1059 : : {
1060 : 74119 : if (TREE_CODE (e.value) == TARGET_EXPR
1061 : 74119 : && !TARGET_EXPR_ELIDING_P (e.value))
1062 : : return true;
1063 : : }
1064 : : return false;
1065 : : }
1066 : :
1067 : : /* If we might need to clean up a partially constructed object, break down the
1068 : : CONSTRUCTOR with split_nonconstant_init. Also expand VEC_INIT_EXPR at this
1069 : : point. If initializing TO with FROM is non-trivial, overwrite *REPLACE with
1070 : : the result. */
1071 : :
1072 : : static void
1073 : 61000330 : cp_genericize_init (tree *replace, tree from, tree to, vec<tree,va_gc>** flags)
1074 : : {
1075 : 61000330 : tree init = NULL_TREE;
1076 : 61000330 : if (TREE_CODE (from) == VEC_INIT_EXPR)
1077 : 1034 : init = expand_vec_init_expr (to, from, tf_warning_or_error, flags);
1078 : 60999296 : else if (TREE_CODE (from) == CONSTRUCTOR
1079 : 2396576 : && TREE_SIDE_EFFECTS (from)
1080 : 61048531 : && ((flag_exceptions
1081 : 49199 : && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (from)))
1082 : 47884 : || any_non_eliding_target_exprs (from)))
1083 : : {
1084 : 1354 : to = cp_stabilize_reference (to);
1085 : 1354 : replace_placeholders (from, to);
1086 : 1354 : init = split_nonconstant_init (to, from);
1087 : : }
1088 : :
1089 : 2388 : if (init)
1090 : : {
1091 : 2388 : if (*replace == from)
1092 : : /* Make cp_gimplify_init_expr call replace_decl on this
1093 : : TARGET_EXPR_INITIAL. */
1094 : 360 : init = fold_convert (void_type_node, init);
1095 : 2388 : *replace = init;
1096 : : }
1097 : 61000330 : }
1098 : :
1099 : : /* For an INIT_EXPR, replace the INIT_EXPR itself. */
1100 : :
1101 : : static void
1102 : 47312622 : cp_genericize_init_expr (tree *stmt_p)
1103 : : {
1104 : 47312622 : iloc_sentinel ils = EXPR_LOCATION (*stmt_p);
1105 : 47312622 : tree to = TREE_OPERAND (*stmt_p, 0);
1106 : 47312622 : tree from = TREE_OPERAND (*stmt_p, 1);
1107 : 5004808 : if (SIMPLE_TARGET_EXPR_P (from)
1108 : : /* Return gets confused if we clobber its INIT_EXPR this soon. */
1109 : 50935222 : && TREE_CODE (to) != RESULT_DECL)
1110 : 68868 : from = TARGET_EXPR_INITIAL (from);
1111 : 47312622 : cp_genericize_init (stmt_p, from, to, nullptr);
1112 : 47312622 : }
1113 : :
1114 : : /* For a TARGET_EXPR, change the TARGET_EXPR_INITIAL. We will need to use
1115 : : replace_decl later when we know what we're initializing. */
1116 : :
1117 : : static void
1118 : 13687708 : cp_genericize_target_expr (tree *stmt_p)
1119 : : {
1120 : 13687708 : iloc_sentinel ils = EXPR_LOCATION (*stmt_p);
1121 : 13687708 : tree slot = TARGET_EXPR_SLOT (*stmt_p);
1122 : 13687708 : vec<tree, va_gc> *flags = make_tree_vector ();
1123 : 13687708 : cp_genericize_init (&TARGET_EXPR_INITIAL (*stmt_p),
1124 : 13687708 : TARGET_EXPR_INITIAL (*stmt_p), slot, &flags);
1125 : 13687708 : gcc_assert (!DECL_INITIAL (slot));
1126 : 41063156 : for (tree f : flags)
1127 : : {
1128 : : /* Once initialization is complete TARGET_EXPR_CLEANUP becomes active, so
1129 : : disable any subobject cleanups. */
1130 : 32 : tree d = build_disable_temp_cleanup (f);
1131 : 32 : auto &r = TARGET_EXPR_INITIAL (*stmt_p);
1132 : 32 : r = add_stmt_to_compound (r, d);
1133 : : }
1134 : 13687708 : release_tree_vector (flags);
1135 : 13687708 : }
1136 : :
1137 : : /* Similar to if (target_expr_needs_replace) replace_decl, but TP is the
1138 : : TARGET_EXPR_INITIAL, and this also updates *_SLOT. We need this extra
1139 : : replacement when cp_folding TARGET_EXPR to preserve the invariant that
1140 : : AGGR_INIT_EXPR_SLOT agrees with the enclosing TARGET_EXPR_SLOT. */
1141 : :
1142 : : static bool
1143 : 9947 : maybe_replace_decl (tree *tp, tree decl, tree replacement)
1144 : : {
1145 : 9947 : if (!*tp || !VOID_TYPE_P (TREE_TYPE (*tp)))
1146 : : return false;
1147 : : tree t = *tp;
1148 : 61 : while (TREE_CODE (t) == COMPOUND_EXPR)
1149 : 0 : t = TREE_OPERAND (t, 1);
1150 : 61 : if (TREE_CODE (t) == AGGR_INIT_EXPR)
1151 : 61 : replace_decl (&AGGR_INIT_EXPR_SLOT (t), decl, replacement);
1152 : 0 : else if (TREE_CODE (t) == VEC_INIT_EXPR)
1153 : 0 : replace_decl (&VEC_INIT_EXPR_SLOT (t), decl, replacement);
1154 : : else
1155 : 0 : replace_decl (tp, decl, replacement);
1156 : : return true;
1157 : : }
1158 : :
1159 : : /* Genericization context. */
1160 : :
1161 : 39384797 : struct cp_genericize_data
1162 : : {
1163 : : hash_set<tree> *p_set;
1164 : : auto_vec<tree> bind_expr_stack;
1165 : : struct cp_genericize_omp_taskreg *omp_ctx;
1166 : : tree try_block;
1167 : : bool no_sanitize_p;
1168 : : bool handle_invisiref_parm_p;
1169 : : };
1170 : :
1171 : : /* Emit an error about taking the address of an immediate function.
1172 : : EXPR is the whole expression; DECL is the immediate function. */
1173 : :
1174 : : static void
1175 : 63 : taking_address_of_imm_fn_error (tree expr, tree decl)
1176 : : {
1177 : 63 : auto_diagnostic_group d;
1178 : 63 : const location_t loc = (TREE_CODE (expr) == PTRMEM_CST
1179 : 63 : ? PTRMEM_CST_LOCATION (expr)
1180 : 54 : : EXPR_LOCATION (expr));
1181 : 63 : error_at (loc, "taking address of an immediate function %qD", decl);
1182 : 63 : maybe_explain_promoted_consteval (loc, decl);
1183 : 63 : }
1184 : :
1185 : : /* Build up an INIT_EXPR to initialize the object of a constructor call that
1186 : : has been folded to a constant value. CALL is the CALL_EXPR for the
1187 : : constructor call; INIT is the value. */
1188 : :
1189 : : static tree
1190 : 69948 : cp_build_init_expr_for_ctor (tree call, tree init)
1191 : : {
1192 : 69948 : tree a = CALL_EXPR_ARG (call, 0);
1193 : 69948 : if (is_dummy_object (a))
1194 : : return init;
1195 : 69948 : const bool return_this = targetm.cxx.cdtor_returns_this ();
1196 : 69948 : const location_t loc = EXPR_LOCATION (call);
1197 : 69948 : if (return_this)
1198 : 0 : a = cp_save_expr (a);
1199 : 69948 : tree s = build_fold_indirect_ref_loc (loc, a);
1200 : 69948 : init = cp_build_init_expr (s, init);
1201 : 69948 : if (return_this)
1202 : 0 : init = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (call), init,
1203 : 0 : fold_convert_loc (loc, TREE_TYPE (call), a));
1204 : : return init;
1205 : : }
1206 : :
1207 : : /* A subroutine of cp_fold_r to handle immediate functions. */
1208 : :
1209 : : static tree
1210 : 778815481 : cp_fold_immediate_r (tree *stmt_p, int *walk_subtrees, void *data_)
1211 : : {
1212 : 778815481 : auto data = static_cast<cp_fold_data *>(data_);
1213 : 778815481 : tree stmt = *stmt_p;
1214 : : /* The purpose of this is not to emit errors for mce_unknown. */
1215 : 778815481 : const tsubst_flags_t complain = (data->flags & ff_mce_false
1216 : 778815481 : ? tf_error : tf_none);
1217 : 778815481 : const tree_code code = TREE_CODE (stmt);
1218 : :
1219 : : /* No need to look into types or unevaluated operands.
1220 : : NB: This affects cp_fold_r as well. */
1221 : 778815481 : if (TYPE_P (stmt)
1222 : 777840884 : || unevaluated_p (code)
1223 : : /* We do not use in_immediate_context here because it checks
1224 : : more than is desirable, e.g., sk_template_parms. */
1225 : 777765840 : || cp_unevaluated_operand
1226 : 1556581173 : || (current_function_decl
1227 : 1513967336 : && DECL_IMMEDIATE_FUNCTION_P (current_function_decl)))
1228 : : {
1229 : 1054450 : *walk_subtrees = 0;
1230 : 1054450 : return NULL_TREE;
1231 : : }
1232 : :
1233 : 777761031 : tree decl = NULL_TREE;
1234 : 777761031 : bool call_p = false;
1235 : :
1236 : : /* We are looking for &fn or fn(). */
1237 : 777761031 : switch (code)
1238 : : {
1239 : 39443307 : case CALL_EXPR:
1240 : 39443307 : case AGGR_INIT_EXPR:
1241 : 39443307 : if (tree fn = cp_get_callee (stmt))
1242 : 39231001 : if (TREE_CODE (fn) != ADDR_EXPR || ADDR_EXPR_DENOTES_CALL_P (fn))
1243 : 38133897 : decl = cp_get_fndecl_from_callee (fn, /*fold*/false);
1244 : 38133897 : call_p = true;
1245 : 38133897 : break;
1246 : 8243 : case PTRMEM_CST:
1247 : 8243 : decl = PTRMEM_CST_MEMBER (stmt);
1248 : 8243 : break;
1249 : 63517929 : case ADDR_EXPR:
1250 : 63517929 : if (!ADDR_EXPR_DENOTES_CALL_P (stmt))
1251 : 26152494 : decl = TREE_OPERAND (stmt, 0);
1252 : : break;
1253 : : default:
1254 : : return NULL_TREE;
1255 : : }
1256 : :
1257 : 64294634 : if (!decl || TREE_CODE (decl) != FUNCTION_DECL)
1258 : 63932746 : return NULL_TREE;
1259 : :
1260 : : /* Fully escalate once all templates have been instantiated. What we're
1261 : : calling is not a consteval function but it may become one. This
1262 : : requires recursing; DECL may be promoted to consteval because it
1263 : : contains an escalating expression E, but E itself may have to be
1264 : : promoted first, etc. */
1265 : 39036733 : if (at_eof > 1 && unchecked_immediate_escalating_function_p (decl))
1266 : : {
1267 : : /* Set before the actual walk to avoid endless recursion. */
1268 : 1127637 : DECL_ESCALATION_CHECKED_P (decl) = true;
1269 : : /* We're only looking for the first escalating expression. Let us not
1270 : : walk more trees than necessary, hence mce_unknown. */
1271 : 1127637 : cp_fold_immediate (&DECL_SAVED_TREE (decl), mce_unknown, decl);
1272 : : }
1273 : :
1274 : : /* [expr.const]p16 "An expression or conversion is immediate-escalating if
1275 : : it is not initially in an immediate function context and it is either
1276 : : -- an immediate invocation that is not a constant expression and is not
1277 : : a subexpression of an immediate invocation."
1278 : :
1279 : : If we are in an immediate-escalating function, the immediate-escalating
1280 : : expression or conversion makes it an immediate function. So STMT does
1281 : : not need to produce a constant expression. */
1282 : 78073466 : if (DECL_IMMEDIATE_FUNCTION_P (decl))
1283 : : {
1284 : 102540 : tree e = cxx_constant_value (stmt, tf_none);
1285 : 102540 : if (e == error_mark_node)
1286 : : {
1287 : : /* This takes care of, e.g.,
1288 : : template <typename T>
1289 : : constexpr int f(T t)
1290 : : {
1291 : : return id(t);
1292 : : }
1293 : : where id (consteval) causes f<int> to be promoted. */
1294 : 602 : if (immediate_escalating_function_p (current_function_decl))
1295 : 183 : promote_function_to_consteval (current_function_decl);
1296 : 419 : else if (complain & tf_error)
1297 : : {
1298 : 317 : if (call_p)
1299 : : {
1300 : 266 : auto_diagnostic_group d;
1301 : 266 : location_t loc = cp_expr_loc_or_input_loc (stmt);
1302 : 266 : error_at (loc, "call to consteval function %qE is "
1303 : : "not a constant expression", stmt);
1304 : : /* Explain why it's not a constant expression. */
1305 : 266 : *stmt_p = cxx_constant_value (stmt, complain);
1306 : 266 : maybe_explain_promoted_consteval (loc, decl);
1307 : 266 : }
1308 : 51 : else if (!data->pset.add (stmt))
1309 : : {
1310 : 51 : taking_address_of_imm_fn_error (stmt, decl);
1311 : 51 : *stmt_p = build_zero_cst (TREE_TYPE (stmt));
1312 : : }
1313 : : /* If we're giving hard errors, continue the walk rather than
1314 : : bailing out after the first error. */
1315 : 317 : return NULL_TREE;
1316 : : }
1317 : 285 : *walk_subtrees = 0;
1318 : 285 : return stmt;
1319 : : }
1320 : : /* We've evaluated the consteval function call. */
1321 : 101938 : if (call_p)
1322 : : {
1323 : 103070 : if (code == CALL_EXPR && DECL_CONSTRUCTOR_P (decl))
1324 : 6 : *stmt_p = cp_build_init_expr_for_ctor (stmt, e);
1325 : : else
1326 : 101932 : *stmt_p = e;
1327 : : }
1328 : : }
1329 : : /* We've encountered a function call that may turn out to be consteval
1330 : : later. Store its caller so that we can ensure that the call is
1331 : : a constant expression. */
1332 : 38934193 : else if (unchecked_immediate_escalating_function_p (decl))
1333 : : {
1334 : : /* Make sure we're not inserting new elements while walking
1335 : : the deferred_escalating_exprs hash table; if we are, it's
1336 : : likely that a function wasn't properly marked checked for
1337 : : i-e expressions. */
1338 : 4518614 : gcc_checking_assert (at_eof <= 1);
1339 : 4518614 : if (current_function_decl)
1340 : 4518416 : remember_escalating_expr (current_function_decl);
1341 : : /* auto p = &f<int>; in the global scope won't be ensconced in
1342 : : a function we could store for later at this point. (If there's
1343 : : no c_f_d at this point and we're dealing with a call, we should
1344 : : see the call when cp_fold_function __static_i_and_d.) */
1345 : 198 : else if (!call_p)
1346 : 77 : remember_escalating_expr (stmt);
1347 : : }
1348 : :
1349 : : return NULL_TREE;
1350 : : }
1351 : :
1352 : : /* Perform any pre-gimplification folding of C++ front end trees to
1353 : : GENERIC.
1354 : : Note: The folding of non-omp cases is something to move into
1355 : : the middle-end. As for now we have most foldings only on GENERIC
1356 : : in fold-const, we need to perform this before transformation to
1357 : : GIMPLE-form.
1358 : :
1359 : : ??? This is algorithmically weird because walk_tree works in pre-order, so
1360 : : we see outer expressions before inner expressions. This isn't as much of an
1361 : : issue because cp_fold recurses into subexpressions in many cases, but then
1362 : : walk_tree walks back into those subexpressions again. We avoid the
1363 : : resulting complexity problem by caching the result of cp_fold, but it's
1364 : : inelegant. */
1365 : :
1366 : : static tree
1367 : 1754014383 : cp_fold_r (tree *stmt_p, int *walk_subtrees, void *data_)
1368 : : {
1369 : 1754014383 : cp_fold_data *data = (cp_fold_data*)data_;
1370 : 1754014383 : tree stmt = *stmt_p;
1371 : 1754014383 : enum tree_code code = TREE_CODE (stmt);
1372 : :
1373 : 1754014383 : if (cxx_dialect >= cxx20)
1374 : : {
1375 : : /* Unfortunately we must handle code like
1376 : : false ? bar () : 42
1377 : : where we have to check bar too. The cp_fold call below could
1378 : : fold the ?: into a constant before we've checked it. */
1379 : 597240864 : if (code == COND_EXPR)
1380 : : {
1381 : 983026 : auto then_fn = cp_fold_r, else_fn = cp_fold_r;
1382 : : /* See if we can figure out if either of the branches is dead. If it
1383 : : is, we don't need to do everything that cp_fold_r does. */
1384 : 983026 : cp_walk_tree (&TREE_OPERAND (stmt, 0), cp_fold_r, data, nullptr);
1385 : 983026 : if (integer_zerop (TREE_OPERAND (stmt, 0)))
1386 : : then_fn = cp_fold_immediate_r;
1387 : 954299 : else if (integer_nonzerop (TREE_OPERAND (stmt, 0)))
1388 : 18368 : else_fn = cp_fold_immediate_r;
1389 : :
1390 : 983026 : if (TREE_OPERAND (stmt, 1))
1391 : 983026 : cp_walk_tree (&TREE_OPERAND (stmt, 1), then_fn, data,
1392 : : nullptr);
1393 : 983026 : if (TREE_OPERAND (stmt, 2))
1394 : 983011 : cp_walk_tree (&TREE_OPERAND (stmt, 2), else_fn, data,
1395 : : nullptr);
1396 : 983026 : *walk_subtrees = 0;
1397 : : /* Don't return yet, still need the cp_fold below. */
1398 : : }
1399 : : else
1400 : 596257838 : cp_fold_immediate_r (stmt_p, walk_subtrees, data);
1401 : : }
1402 : :
1403 : 1754014383 : *stmt_p = stmt = cp_fold (*stmt_p, data->flags);
1404 : :
1405 : : /* For certain trees, like +foo(), the cp_fold above will remove the +,
1406 : : and the subsequent tree walk would go straight down to the CALL_EXPR's
1407 : : operands, meaning that cp_fold_immediate_r would never see the
1408 : : CALL_EXPR. Ew :(. */
1409 : 1754014383 : if (TREE_CODE (stmt) == CALL_EXPR && code != CALL_EXPR)
1410 : 105200 : cp_fold_immediate_r (stmt_p, walk_subtrees, data);
1411 : :
1412 : 1754014383 : if (data->pset.add (stmt))
1413 : : {
1414 : : /* Don't walk subtrees of stmts we've already walked once, otherwise
1415 : : we can have exponential complexity with e.g. lots of nested
1416 : : SAVE_EXPRs or TARGET_EXPRs. cp_fold uses a cache and will return
1417 : : always the same tree, which the first time cp_fold_r has been
1418 : : called on it had the subtrees walked. */
1419 : 267450882 : *walk_subtrees = 0;
1420 : 267450882 : return NULL_TREE;
1421 : : }
1422 : :
1423 : 1486563501 : code = TREE_CODE (stmt);
1424 : 1486563501 : switch (code)
1425 : : {
1426 : 25854 : tree x;
1427 : 25854 : int i, n;
1428 : 25854 : case OMP_FOR:
1429 : 25854 : case OMP_SIMD:
1430 : 25854 : case OMP_DISTRIBUTE:
1431 : 25854 : case OMP_LOOP:
1432 : 25854 : case OMP_TASKLOOP:
1433 : 25854 : case OMP_TILE:
1434 : 25854 : case OMP_UNROLL:
1435 : 25854 : case OACC_LOOP:
1436 : 25854 : cp_walk_tree (&OMP_FOR_BODY (stmt), cp_fold_r, data, NULL);
1437 : 25854 : cp_walk_tree (&OMP_FOR_CLAUSES (stmt), cp_fold_r, data, NULL);
1438 : 25854 : cp_walk_tree (&OMP_FOR_INIT (stmt), cp_fold_r, data, NULL);
1439 : 25854 : x = OMP_FOR_COND (stmt);
1440 : 25854 : if (x && TREE_CODE_CLASS (TREE_CODE (x)) == tcc_comparison)
1441 : : {
1442 : 0 : cp_walk_tree (&TREE_OPERAND (x, 0), cp_fold_r, data, NULL);
1443 : 0 : cp_walk_tree (&TREE_OPERAND (x, 1), cp_fold_r, data, NULL);
1444 : : }
1445 : 19061 : else if (x && TREE_CODE (x) == TREE_VEC)
1446 : : {
1447 : 19061 : n = TREE_VEC_LENGTH (x);
1448 : 43765 : for (i = 0; i < n; i++)
1449 : : {
1450 : 24704 : tree o = TREE_VEC_ELT (x, i);
1451 : 24704 : if (o && TREE_CODE_CLASS (TREE_CODE (o)) == tcc_comparison)
1452 : 23791 : cp_walk_tree (&TREE_OPERAND (o, 1), cp_fold_r, data, NULL);
1453 : : }
1454 : : }
1455 : 25854 : x = OMP_FOR_INCR (stmt);
1456 : 25854 : if (x && TREE_CODE (x) == TREE_VEC)
1457 : : {
1458 : 19061 : n = TREE_VEC_LENGTH (x);
1459 : 43765 : for (i = 0; i < n; i++)
1460 : : {
1461 : 24704 : tree o = TREE_VEC_ELT (x, i);
1462 : 24704 : if (o && TREE_CODE (o) == MODIFY_EXPR)
1463 : 6010 : o = TREE_OPERAND (o, 1);
1464 : 23791 : if (o && (TREE_CODE (o) == PLUS_EXPR || TREE_CODE (o) == MINUS_EXPR
1465 : 19228 : || TREE_CODE (o) == POINTER_PLUS_EXPR))
1466 : : {
1467 : 6010 : cp_walk_tree (&TREE_OPERAND (o, 0), cp_fold_r, data, NULL);
1468 : 6010 : cp_walk_tree (&TREE_OPERAND (o, 1), cp_fold_r, data, NULL);
1469 : : }
1470 : : }
1471 : : }
1472 : 25854 : cp_walk_tree (&OMP_FOR_PRE_BODY (stmt), cp_fold_r, data, NULL);
1473 : 25854 : *walk_subtrees = 0;
1474 : 25854 : return NULL_TREE;
1475 : :
1476 : 15789961 : case IF_STMT:
1477 : 15789961 : if (IF_STMT_CONSTEVAL_P (stmt))
1478 : : {
1479 : : /* Don't walk THEN_CLAUSE (stmt) for consteval if. IF_COND is always
1480 : : boolean_false_node. */
1481 : 9881 : cp_walk_tree (&ELSE_CLAUSE (stmt), cp_fold_r, data, NULL);
1482 : 9881 : cp_walk_tree (&IF_SCOPE (stmt), cp_fold_r, data, NULL);
1483 : 9881 : *walk_subtrees = 0;
1484 : 9881 : return NULL_TREE;
1485 : : }
1486 : : break;
1487 : :
1488 : : /* cp_genericize_{init,target}_expr are only for genericize time; they're
1489 : : here rather than in cp_genericize to avoid problems with the invisible
1490 : : reference transition. */
1491 : 47321823 : case INIT_EXPR:
1492 : 47321823 : if (data->flags & ff_genericize)
1493 : 47312622 : cp_genericize_init_expr (stmt_p);
1494 : : break;
1495 : :
1496 : 14118006 : case TARGET_EXPR:
1497 : 14118006 : if (data->flags & ff_genericize)
1498 : 13687708 : cp_genericize_target_expr (stmt_p);
1499 : :
1500 : 14118006 : if (tree &init = TARGET_EXPR_INITIAL (stmt))
1501 : : {
1502 : 14118006 : cp_walk_tree (&init, cp_fold_r, data, NULL);
1503 : 14118006 : cp_walk_tree (&TARGET_EXPR_CLEANUP (stmt), cp_fold_r, data, NULL);
1504 : 14118006 : *walk_subtrees = 0;
1505 : 14118006 : if (!flag_no_inline)
1506 : : {
1507 : 13611763 : tree folded = maybe_constant_init (init, TARGET_EXPR_SLOT (stmt));
1508 : 13611763 : if (folded != init && TREE_CONSTANT (folded))
1509 : 657886 : init = folded;
1510 : : }
1511 : : /* Folding might replace e.g. a COND_EXPR with a TARGET_EXPR; in
1512 : : that case, strip it in favor of this one. */
1513 : 14118006 : if (TREE_CODE (init) == TARGET_EXPR)
1514 : : {
1515 : 9947 : tree sub = TARGET_EXPR_INITIAL (init);
1516 : 9947 : maybe_replace_decl (&sub, TARGET_EXPR_SLOT (init),
1517 : 9947 : TARGET_EXPR_SLOT (stmt));
1518 : 9947 : init = sub;
1519 : : }
1520 : : }
1521 : : break;
1522 : :
1523 : : default:
1524 : : break;
1525 : : }
1526 : :
1527 : : return NULL_TREE;
1528 : : }
1529 : :
1530 : : /* Fold ALL the trees! FIXME we should be able to remove this, but
1531 : : apparently that still causes optimization regressions. */
1532 : :
1533 : : void
1534 : 52442026 : cp_fold_function (tree fndecl)
1535 : : {
1536 : : /* By now all manifestly-constant-evaluated expressions will have
1537 : : been constant-evaluated already if possible, so we can safely
1538 : : pass ff_mce_false. */
1539 : 52442026 : cp_fold_data data (ff_genericize | ff_mce_false);
1540 : 52442026 : cp_walk_tree (&DECL_SAVED_TREE (fndecl), cp_fold_r, &data, NULL);
1541 : :
1542 : : /* This is merely an optimization: if FNDECL has no i-e expressions,
1543 : : we'll not save c_f_d, and we can safely say that FNDECL will not
1544 : : be promoted to consteval. */
1545 : 52442026 : if (deferred_escalating_exprs
1546 : 52442026 : && !deferred_escalating_exprs->contains (current_function_decl))
1547 : 13086779 : DECL_ESCALATION_CHECKED_P (fndecl) = true;
1548 : 52442026 : }
1549 : :
1550 : : /* We've stashed immediate-escalating functions. Now see if they indeed
1551 : : ought to be promoted to consteval. */
1552 : :
1553 : : void
1554 : 91683 : process_and_check_pending_immediate_escalating_fns ()
1555 : : {
1556 : : /* This will be null for -fno-immediate-escalation. */
1557 : 91683 : if (!deferred_escalating_exprs)
1558 : : return;
1559 : :
1560 : 5275550 : for (auto e : *deferred_escalating_exprs)
1561 : 2635004 : if (TREE_CODE (e) == FUNCTION_DECL && !DECL_ESCALATION_CHECKED_P (e))
1562 : 1832193 : cp_fold_immediate (&DECL_SAVED_TREE (e), mce_false, e);
1563 : :
1564 : : /* We've escalated every function that could have been promoted to
1565 : : consteval. Check that we are not taking the address of a consteval
1566 : : function. */
1567 : 5275627 : for (auto e : *deferred_escalating_exprs)
1568 : : {
1569 : 2635004 : if (TREE_CODE (e) == FUNCTION_DECL)
1570 : 2634927 : continue;
1571 : 77 : tree decl = (TREE_CODE (e) == PTRMEM_CST
1572 : 154 : ? PTRMEM_CST_MEMBER (e)
1573 : 77 : : TREE_OPERAND (e, 0));
1574 : 154 : if (DECL_IMMEDIATE_FUNCTION_P (decl))
1575 : 12 : taking_address_of_imm_fn_error (e, decl);
1576 : : }
1577 : :
1578 : 5619 : deferred_escalating_exprs = nullptr;
1579 : : }
1580 : :
1581 : : /* Turn SPACESHIP_EXPR EXPR into GENERIC. */
1582 : :
1583 : 60712 : static tree genericize_spaceship (tree expr)
1584 : : {
1585 : 60712 : iloc_sentinel s (cp_expr_location (expr));
1586 : 60712 : tree type = TREE_TYPE (expr);
1587 : 60712 : tree op0 = TREE_OPERAND (expr, 0);
1588 : 60712 : tree op1 = TREE_OPERAND (expr, 1);
1589 : 60712 : return genericize_spaceship (input_location, type, op0, op1);
1590 : 60712 : }
1591 : :
1592 : : /* If EXPR involves an anonymous VLA type, prepend a DECL_EXPR for that type
1593 : : to trigger gimplify_type_sizes; otherwise a cast to pointer-to-VLA confuses
1594 : : the middle-end (c++/88256). If EXPR is a DECL, use add_stmt and return
1595 : : NULL_TREE; otherwise return a COMPOUND_STMT of the DECL_EXPR and EXPR. */
1596 : :
1597 : : tree
1598 : 111435470 : predeclare_vla (tree expr)
1599 : : {
1600 : 111435470 : tree type = TREE_TYPE (expr);
1601 : 111435470 : if (type == error_mark_node)
1602 : : return expr;
1603 : 111435387 : if (is_typedef_decl (expr))
1604 : 111435387 : type = DECL_ORIGINAL_TYPE (expr);
1605 : :
1606 : : /* We need to strip pointers for gimplify_type_sizes. */
1607 : 111435387 : tree vla = type;
1608 : 165741004 : while (POINTER_TYPE_P (vla))
1609 : : {
1610 : 56391813 : if (TYPE_NAME (vla))
1611 : : return expr;
1612 : 54305617 : vla = TREE_TYPE (vla);
1613 : : }
1614 : 52782689 : if (vla == type || TYPE_NAME (vla)
1615 : 109835880 : || !variably_modified_type_p (vla, NULL_TREE))
1616 : 109349052 : return expr;
1617 : :
1618 : 139 : tree decl = build_decl (input_location, TYPE_DECL, NULL_TREE, vla);
1619 : 139 : DECL_ARTIFICIAL (decl) = 1;
1620 : 139 : TYPE_NAME (vla) = decl;
1621 : 139 : tree dexp = build_stmt (input_location, DECL_EXPR, decl);
1622 : 139 : if (DECL_P (expr))
1623 : : {
1624 : 5 : add_stmt (dexp);
1625 : 5 : return NULL_TREE;
1626 : : }
1627 : : else
1628 : : {
1629 : 134 : expr = build2 (COMPOUND_EXPR, type, dexp, expr);
1630 : 134 : return expr;
1631 : : }
1632 : : }
1633 : :
1634 : : /* Perform any pre-gimplification lowering of C++ front end trees to
1635 : : GENERIC. */
1636 : :
1637 : : static tree
1638 : 1419320066 : cp_genericize_r (tree *stmt_p, int *walk_subtrees, void *data)
1639 : : {
1640 : 1435111452 : tree stmt = *stmt_p;
1641 : 1435111452 : struct cp_genericize_data *wtd = (struct cp_genericize_data *) data;
1642 : 1435111452 : hash_set<tree> *p_set = wtd->p_set;
1643 : :
1644 : : /* If in an OpenMP context, note var uses. */
1645 : 1435111452 : if (UNLIKELY (wtd->omp_ctx != NULL)
1646 : 580804 : && (VAR_P (stmt)
1647 : : || TREE_CODE (stmt) == PARM_DECL
1648 : : || TREE_CODE (stmt) == RESULT_DECL)
1649 : 1435218502 : && omp_var_to_track (stmt))
1650 : 12270 : omp_cxx_notice_variable (wtd->omp_ctx, stmt);
1651 : :
1652 : : /* Don't dereference parms in a thunk, pass the references through. */
1653 : 59489928 : if ((TREE_CODE (stmt) == CALL_EXPR && call_from_lambda_thunk_p (stmt))
1654 : 1494570479 : || (TREE_CODE (stmt) == AGGR_INIT_EXPR && AGGR_INIT_FROM_THUNK_P (stmt)))
1655 : : {
1656 : 31051 : *walk_subtrees = 0;
1657 : 31051 : return NULL;
1658 : : }
1659 : :
1660 : : /* Dereference invisible reference parms. */
1661 : 1435080401 : if (wtd->handle_invisiref_parm_p && is_invisiref_parm (stmt))
1662 : : {
1663 : 1111559 : *stmt_p = convert_from_reference (stmt);
1664 : 1111559 : p_set->add (*stmt_p);
1665 : 1111559 : *walk_subtrees = 0;
1666 : 1111559 : return NULL;
1667 : : }
1668 : :
1669 : : /* Map block scope extern declarations to visible declarations with the
1670 : : same name and type in outer scopes if any. */
1671 : 1433968842 : if (VAR_OR_FUNCTION_DECL_P (stmt) && DECL_LOCAL_DECL_P (stmt))
1672 : 19936 : if (tree alias = DECL_LOCAL_DECL_ALIAS (stmt))
1673 : : {
1674 : 19936 : if (alias != error_mark_node)
1675 : : {
1676 : 19933 : *stmt_p = alias;
1677 : 19933 : TREE_USED (alias) |= TREE_USED (stmt);
1678 : : }
1679 : 19936 : *walk_subtrees = 0;
1680 : 19936 : return NULL;
1681 : : }
1682 : :
1683 : 1433948906 : if (TREE_CODE (stmt) == INTEGER_CST
1684 : 171581056 : && TYPE_REF_P (TREE_TYPE (stmt))
1685 : 146 : && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))
1686 : 1433948907 : && !wtd->no_sanitize_p)
1687 : : {
1688 : 1 : ubsan_maybe_instrument_reference (stmt_p);
1689 : 1 : if (*stmt_p != stmt)
1690 : : {
1691 : 1 : *walk_subtrees = 0;
1692 : 1 : return NULL_TREE;
1693 : : }
1694 : : }
1695 : :
1696 : : /* Other than invisiref parms, don't walk the same tree twice. */
1697 : 1433948905 : if (p_set->contains (stmt))
1698 : : {
1699 : 261842060 : *walk_subtrees = 0;
1700 : 261842060 : return NULL_TREE;
1701 : : }
1702 : :
1703 : 1172106845 : switch (TREE_CODE (stmt))
1704 : : {
1705 : 92620893 : case ADDR_EXPR:
1706 : 92620893 : if (is_invisiref_parm (TREE_OPERAND (stmt, 0)))
1707 : : {
1708 : : /* If in an OpenMP context, note var uses. */
1709 : 513405 : if (UNLIKELY (wtd->omp_ctx != NULL)
1710 : 513405 : && omp_var_to_track (TREE_OPERAND (stmt, 0)))
1711 : 412 : omp_cxx_notice_variable (wtd->omp_ctx, TREE_OPERAND (stmt, 0));
1712 : 513405 : *stmt_p = fold_convert (TREE_TYPE (stmt), TREE_OPERAND (stmt, 0));
1713 : 513405 : *walk_subtrees = 0;
1714 : : }
1715 : : break;
1716 : :
1717 : 33832799 : case RETURN_EXPR:
1718 : 33832799 : if (TREE_OPERAND (stmt, 0))
1719 : : {
1720 : 33225453 : if (is_invisiref_parm (TREE_OPERAND (stmt, 0)))
1721 : : /* Don't dereference an invisiref RESULT_DECL inside a
1722 : : RETURN_EXPR. */
1723 : 1559 : *walk_subtrees = 0;
1724 : 33225453 : if (RETURN_EXPR_LOCAL_ADDR_P (stmt))
1725 : : {
1726 : : /* Don't return the address of a local variable. */
1727 : 165 : tree *p = &TREE_OPERAND (stmt, 0);
1728 : 330 : while (TREE_CODE (*p) == COMPOUND_EXPR)
1729 : 0 : p = &TREE_OPERAND (*p, 0);
1730 : 165 : if (TREE_CODE (*p) == INIT_EXPR)
1731 : : {
1732 : 165 : tree op = TREE_OPERAND (*p, 1);
1733 : 165 : tree new_op = build2 (COMPOUND_EXPR, TREE_TYPE (op), op,
1734 : 165 : build_zero_cst (TREE_TYPE (op)));
1735 : 165 : TREE_OPERAND (*p, 1) = new_op;
1736 : : }
1737 : : }
1738 : : }
1739 : : break;
1740 : :
1741 : 81191 : case OMP_CLAUSE:
1742 : 81191 : switch (OMP_CLAUSE_CODE (stmt))
1743 : : {
1744 : 2581 : case OMP_CLAUSE_LASTPRIVATE:
1745 : : /* Don't dereference an invisiref in OpenMP clauses. */
1746 : 2581 : if (is_invisiref_parm (OMP_CLAUSE_DECL (stmt)))
1747 : : {
1748 : 53 : *walk_subtrees = 0;
1749 : 53 : if (OMP_CLAUSE_LASTPRIVATE_STMT (stmt))
1750 : 48 : cp_walk_tree (&OMP_CLAUSE_LASTPRIVATE_STMT (stmt),
1751 : : cp_genericize_r, data, NULL);
1752 : : }
1753 : : break;
1754 : 2043 : case OMP_CLAUSE_PRIVATE:
1755 : : /* Don't dereference an invisiref in OpenMP clauses. */
1756 : 2043 : if (is_invisiref_parm (OMP_CLAUSE_DECL (stmt)))
1757 : 8 : *walk_subtrees = 0;
1758 : 2035 : else if (wtd->omp_ctx != NULL)
1759 : : {
1760 : : /* Private clause doesn't cause any references to the
1761 : : var in outer contexts, avoid calling
1762 : : omp_cxx_notice_variable for it. */
1763 : 584 : struct cp_genericize_omp_taskreg *old = wtd->omp_ctx;
1764 : 584 : wtd->omp_ctx = NULL;
1765 : 584 : cp_walk_tree (&OMP_CLAUSE_DECL (stmt), cp_genericize_r,
1766 : : data, NULL);
1767 : 584 : wtd->omp_ctx = old;
1768 : 584 : *walk_subtrees = 0;
1769 : : }
1770 : : break;
1771 : 6003 : case OMP_CLAUSE_SHARED:
1772 : 6003 : case OMP_CLAUSE_FIRSTPRIVATE:
1773 : 6003 : case OMP_CLAUSE_COPYIN:
1774 : 6003 : case OMP_CLAUSE_COPYPRIVATE:
1775 : 6003 : case OMP_CLAUSE_INCLUSIVE:
1776 : 6003 : case OMP_CLAUSE_EXCLUSIVE:
1777 : : /* Don't dereference an invisiref in OpenMP clauses. */
1778 : 6003 : if (is_invisiref_parm (OMP_CLAUSE_DECL (stmt)))
1779 : 87 : *walk_subtrees = 0;
1780 : : break;
1781 : 8489 : case OMP_CLAUSE_REDUCTION:
1782 : 8489 : case OMP_CLAUSE_IN_REDUCTION:
1783 : 8489 : case OMP_CLAUSE_TASK_REDUCTION:
1784 : : /* Don't dereference an invisiref in reduction clause's
1785 : : OMP_CLAUSE_DECL either. OMP_CLAUSE_REDUCTION_{INIT,MERGE}
1786 : : still needs to be genericized. */
1787 : 8489 : if (is_invisiref_parm (OMP_CLAUSE_DECL (stmt)))
1788 : : {
1789 : 38 : *walk_subtrees = 0;
1790 : 38 : if (OMP_CLAUSE_REDUCTION_INIT (stmt))
1791 : 38 : cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (stmt),
1792 : : cp_genericize_r, data, NULL);
1793 : 38 : if (OMP_CLAUSE_REDUCTION_MERGE (stmt))
1794 : 38 : cp_walk_tree (&OMP_CLAUSE_REDUCTION_MERGE (stmt),
1795 : : cp_genericize_r, data, NULL);
1796 : : }
1797 : : break;
1798 : : default:
1799 : : break;
1800 : : }
1801 : : break;
1802 : :
1803 : : /* Due to the way voidify_wrapper_expr is written, we don't get a chance
1804 : : to lower this construct before scanning it, so we need to lower these
1805 : : before doing anything else. */
1806 : 5032093 : case CLEANUP_STMT:
1807 : 5032093 : *stmt_p = build2_loc (EXPR_LOCATION (stmt),
1808 : 5032093 : CLEANUP_EH_ONLY (stmt) ? TRY_CATCH_EXPR
1809 : : : TRY_FINALLY_EXPR,
1810 : : void_type_node,
1811 : 5032093 : CLEANUP_BODY (stmt),
1812 : 5032093 : CLEANUP_EXPR (stmt));
1813 : 5032093 : break;
1814 : :
1815 : 15790631 : case IF_STMT:
1816 : 15790631 : genericize_if_stmt (stmt_p);
1817 : : /* *stmt_p has changed, tail recurse to handle it again. */
1818 : 15790631 : return cp_genericize_r (stmt_p, walk_subtrees, data);
1819 : :
1820 : : /* COND_EXPR might have incompatible types in branches if one or both
1821 : : arms are bitfields. Fix it up now. */
1822 : 15356484 : case COND_EXPR:
1823 : 15356484 : {
1824 : 15356484 : tree type_left
1825 : 15356484 : = (TREE_OPERAND (stmt, 1)
1826 : 15356484 : ? is_bitfield_expr_with_lowered_type (TREE_OPERAND (stmt, 1))
1827 : 15356484 : : NULL_TREE);
1828 : 15356484 : tree type_right
1829 : 15356484 : = (TREE_OPERAND (stmt, 2)
1830 : 15356484 : ? is_bitfield_expr_with_lowered_type (TREE_OPERAND (stmt, 2))
1831 : 15356484 : : NULL_TREE);
1832 : 15356484 : if (type_left
1833 : 15356517 : && !useless_type_conversion_p (TREE_TYPE (stmt),
1834 : 33 : TREE_TYPE (TREE_OPERAND (stmt, 1))))
1835 : : {
1836 : 30 : TREE_OPERAND (stmt, 1)
1837 : 30 : = fold_convert (type_left, TREE_OPERAND (stmt, 1));
1838 : 30 : gcc_assert (useless_type_conversion_p (TREE_TYPE (stmt),
1839 : : type_left));
1840 : : }
1841 : 15356484 : if (type_right
1842 : 15356501 : && !useless_type_conversion_p (TREE_TYPE (stmt),
1843 : 17 : TREE_TYPE (TREE_OPERAND (stmt, 2))))
1844 : : {
1845 : 17 : TREE_OPERAND (stmt, 2)
1846 : 17 : = fold_convert (type_right, TREE_OPERAND (stmt, 2));
1847 : 17 : gcc_assert (useless_type_conversion_p (TREE_TYPE (stmt),
1848 : : type_right));
1849 : : }
1850 : : }
1851 : : break;
1852 : :
1853 : 20785594 : case BIND_EXPR:
1854 : 20785594 : if (UNLIKELY (wtd->omp_ctx != NULL))
1855 : : {
1856 : 26565 : tree decl;
1857 : 32791 : for (decl = BIND_EXPR_VARS (stmt); decl; decl = DECL_CHAIN (decl))
1858 : 6226 : if (VAR_P (decl)
1859 : 6178 : && !DECL_EXTERNAL (decl)
1860 : 12404 : && omp_var_to_track (decl))
1861 : : {
1862 : 579 : splay_tree_node n
1863 : 579 : = splay_tree_lookup (wtd->omp_ctx->variables,
1864 : : (splay_tree_key) decl);
1865 : 579 : if (n == NULL)
1866 : 579 : splay_tree_insert (wtd->omp_ctx->variables,
1867 : : (splay_tree_key) decl,
1868 : 579 : TREE_STATIC (decl)
1869 : : ? OMP_CLAUSE_DEFAULT_SHARED
1870 : : : OMP_CLAUSE_DEFAULT_PRIVATE);
1871 : : }
1872 : : }
1873 : 20785594 : if (sanitize_flags_p (SANITIZE_NULL | SANITIZE_ALIGNMENT | SANITIZE_VPTR))
1874 : : {
1875 : : /* The point here is to not sanitize static initializers. */
1876 : 2966 : bool no_sanitize_p = wtd->no_sanitize_p;
1877 : 2966 : wtd->no_sanitize_p = true;
1878 : 2966 : for (tree decl = BIND_EXPR_VARS (stmt);
1879 : 5804 : decl;
1880 : 2838 : decl = DECL_CHAIN (decl))
1881 : 2838 : if (VAR_P (decl)
1882 : 2838 : && TREE_STATIC (decl)
1883 : 2838 : && DECL_INITIAL (decl))
1884 : 9 : cp_walk_tree (&DECL_INITIAL (decl), cp_genericize_r, data, NULL);
1885 : 2966 : wtd->no_sanitize_p = no_sanitize_p;
1886 : : }
1887 : 20785594 : wtd->bind_expr_stack.safe_push (stmt);
1888 : 20785594 : cp_walk_tree (&BIND_EXPR_BODY (stmt),
1889 : : cp_genericize_r, data, NULL);
1890 : 20785594 : wtd->bind_expr_stack.pop ();
1891 : 20785594 : break;
1892 : :
1893 : 755 : case ASSERTION_STMT:
1894 : 755 : case PRECONDITION_STMT:
1895 : 755 : case POSTCONDITION_STMT:
1896 : 755 : {
1897 : 755 : if (tree check = build_contract_check (stmt))
1898 : : {
1899 : 755 : *stmt_p = check;
1900 : 755 : return cp_genericize_r (stmt_p, walk_subtrees, data);
1901 : : }
1902 : :
1903 : : /* If we didn't build a check, replace it with void_node so we don't
1904 : : leak contracts into GENERIC. */
1905 : 0 : *stmt_p = void_node;
1906 : 0 : *walk_subtrees = 0;
1907 : : }
1908 : 0 : break;
1909 : :
1910 : 30092 : case USING_STMT:
1911 : 30092 : {
1912 : 30092 : tree block = NULL_TREE;
1913 : :
1914 : : /* Get the innermost inclosing GIMPLE_BIND that has a non NULL
1915 : : BLOCK, and append an IMPORTED_DECL to its
1916 : : BLOCK_VARS chained list. */
1917 : 30092 : if (wtd->bind_expr_stack.exists ())
1918 : : {
1919 : 30092 : int i;
1920 : 30092 : for (i = wtd->bind_expr_stack.length () - 1; i >= 0; i--)
1921 : 30092 : if ((block = BIND_EXPR_BLOCK (wtd->bind_expr_stack[i])))
1922 : : break;
1923 : : }
1924 : 30092 : if (block)
1925 : : {
1926 : 30092 : tree decl = TREE_OPERAND (stmt, 0);
1927 : 30092 : gcc_assert (decl);
1928 : :
1929 : 30092 : if (undeduced_auto_decl (decl))
1930 : : /* Omit from the GENERIC, the back-end can't handle it. */;
1931 : : else
1932 : : {
1933 : 30089 : tree using_directive = make_node (IMPORTED_DECL);
1934 : 30089 : TREE_TYPE (using_directive) = void_type_node;
1935 : 30089 : DECL_CONTEXT (using_directive) = current_function_decl;
1936 : 60178 : DECL_SOURCE_LOCATION (using_directive)
1937 : 30089 : = cp_expr_loc_or_input_loc (stmt);
1938 : :
1939 : 30089 : IMPORTED_DECL_ASSOCIATED_DECL (using_directive) = decl;
1940 : 30089 : DECL_CHAIN (using_directive) = BLOCK_VARS (block);
1941 : 30089 : BLOCK_VARS (block) = using_directive;
1942 : : }
1943 : : }
1944 : : /* The USING_STMT won't appear in GENERIC. */
1945 : 30092 : *stmt_p = build1 (NOP_EXPR, void_type_node, integer_zero_node);
1946 : 30092 : *walk_subtrees = 0;
1947 : : }
1948 : 30092 : break;
1949 : :
1950 : 22336155 : case DECL_EXPR:
1951 : 22336155 : if (TREE_CODE (DECL_EXPR_DECL (stmt)) == USING_DECL)
1952 : : {
1953 : : /* Using decls inside DECL_EXPRs are just dropped on the floor. */
1954 : 13733 : *stmt_p = build1 (NOP_EXPR, void_type_node, integer_zero_node);
1955 : 13733 : *walk_subtrees = 0;
1956 : : }
1957 : : else
1958 : : {
1959 : 22322422 : tree d = DECL_EXPR_DECL (stmt);
1960 : 22322422 : if (VAR_P (d))
1961 : 44643718 : gcc_assert (CP_DECL_THREAD_LOCAL_P (d) == DECL_THREAD_LOCAL_P (d));
1962 : : }
1963 : : break;
1964 : :
1965 : 11558 : case OMP_PARALLEL:
1966 : 11558 : case OMP_TASK:
1967 : 11558 : case OMP_TASKLOOP:
1968 : 11558 : {
1969 : 11558 : struct cp_genericize_omp_taskreg omp_ctx;
1970 : 11558 : tree c, decl;
1971 : 11558 : splay_tree_node n;
1972 : :
1973 : 11558 : *walk_subtrees = 0;
1974 : 11558 : cp_walk_tree (&OMP_CLAUSES (stmt), cp_genericize_r, data, NULL);
1975 : 11558 : omp_ctx.is_parallel = TREE_CODE (stmt) == OMP_PARALLEL;
1976 : 11558 : omp_ctx.default_shared = omp_ctx.is_parallel;
1977 : 11558 : omp_ctx.outer = wtd->omp_ctx;
1978 : 11558 : omp_ctx.variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
1979 : 11558 : wtd->omp_ctx = &omp_ctx;
1980 : 27460 : for (c = OMP_CLAUSES (stmt); c; c = OMP_CLAUSE_CHAIN (c))
1981 : 15902 : switch (OMP_CLAUSE_CODE (c))
1982 : : {
1983 : 4756 : case OMP_CLAUSE_SHARED:
1984 : 4756 : case OMP_CLAUSE_PRIVATE:
1985 : 4756 : case OMP_CLAUSE_FIRSTPRIVATE:
1986 : 4756 : case OMP_CLAUSE_LASTPRIVATE:
1987 : 4756 : decl = OMP_CLAUSE_DECL (c);
1988 : 4756 : if (decl == error_mark_node || !omp_var_to_track (decl))
1989 : : break;
1990 : 519 : n = splay_tree_lookup (omp_ctx.variables, (splay_tree_key) decl);
1991 : 519 : if (n != NULL)
1992 : : break;
1993 : 1020 : splay_tree_insert (omp_ctx.variables, (splay_tree_key) decl,
1994 : 510 : OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
1995 : : ? OMP_CLAUSE_DEFAULT_SHARED
1996 : : : OMP_CLAUSE_DEFAULT_PRIVATE);
1997 : 510 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_PRIVATE && omp_ctx.outer)
1998 : 91 : omp_cxx_notice_variable (omp_ctx.outer, decl);
1999 : : break;
2000 : 1644 : case OMP_CLAUSE_DEFAULT:
2001 : 1644 : if (OMP_CLAUSE_DEFAULT_KIND (c) == OMP_CLAUSE_DEFAULT_SHARED)
2002 : 728 : omp_ctx.default_shared = true;
2003 : : default:
2004 : : break;
2005 : : }
2006 : 11558 : if (TREE_CODE (stmt) == OMP_TASKLOOP)
2007 : 995 : c_genericize_control_stmt (stmt_p, walk_subtrees, data,
2008 : : cp_genericize_r, cp_walk_subtrees);
2009 : : else
2010 : 10563 : cp_walk_tree (&OMP_BODY (stmt), cp_genericize_r, data, NULL);
2011 : 11558 : wtd->omp_ctx = omp_ctx.outer;
2012 : 11558 : splay_tree_delete (omp_ctx.variables);
2013 : : }
2014 : 11558 : break;
2015 : :
2016 : 6293 : case OMP_TARGET:
2017 : 6293 : cfun->has_omp_target = true;
2018 : 6293 : break;
2019 : :
2020 : 127928 : case TRY_BLOCK:
2021 : 127928 : {
2022 : 127928 : *walk_subtrees = 0;
2023 : 127928 : tree try_block = wtd->try_block;
2024 : 127928 : wtd->try_block = stmt;
2025 : 127928 : cp_walk_tree (&TRY_STMTS (stmt), cp_genericize_r, data, NULL);
2026 : 127928 : wtd->try_block = try_block;
2027 : 127928 : cp_walk_tree (&TRY_HANDLERS (stmt), cp_genericize_r, data, NULL);
2028 : : }
2029 : 127928 : break;
2030 : :
2031 : 17605606 : case MUST_NOT_THROW_EXPR:
2032 : : /* MUST_NOT_THROW_COND might be something else with TM. */
2033 : 17605606 : if (MUST_NOT_THROW_COND (stmt) == NULL_TREE)
2034 : : {
2035 : 17605588 : *walk_subtrees = 0;
2036 : 17605588 : tree try_block = wtd->try_block;
2037 : 17605588 : wtd->try_block = stmt;
2038 : 17605588 : cp_walk_tree (&TREE_OPERAND (stmt, 0), cp_genericize_r, data, NULL);
2039 : 17605588 : wtd->try_block = try_block;
2040 : : }
2041 : : break;
2042 : :
2043 : 121110 : case THROW_EXPR:
2044 : 121110 : {
2045 : 121110 : location_t loc = location_of (stmt);
2046 : 121110 : if (warning_suppressed_p (stmt /* What warning? */))
2047 : : /* Never mind. */;
2048 : 28563 : else if (wtd->try_block)
2049 : : {
2050 : 794 : if (TREE_CODE (wtd->try_block) == MUST_NOT_THROW_EXPR)
2051 : : {
2052 : 18 : auto_diagnostic_group d;
2053 : 31 : if (warning_at (loc, OPT_Wterminate,
2054 : : "%<throw%> will always call %<terminate%>")
2055 : 10 : && cxx_dialect >= cxx11
2056 : 36 : && DECL_DESTRUCTOR_P (current_function_decl))
2057 : 5 : inform (loc, "in C++11 destructors default to %<noexcept%>");
2058 : 18 : }
2059 : : }
2060 : : else
2061 : : {
2062 : 102 : if (warn_cxx11_compat && cxx_dialect < cxx11
2063 : 204 : && DECL_DESTRUCTOR_P (current_function_decl)
2064 : 1 : && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl))
2065 : : == NULL_TREE)
2066 : 27770 : && (get_defaulted_eh_spec (current_function_decl)
2067 : 1 : == empty_except_spec))
2068 : 1 : warning_at (loc, OPT_Wc__11_compat,
2069 : : "in C++11 this %<throw%> will call %<terminate%> "
2070 : : "because destructors default to %<noexcept%>");
2071 : : }
2072 : : }
2073 : : break;
2074 : :
2075 : 34105335 : case CONVERT_EXPR:
2076 : 34105335 : gcc_checking_assert (!AGGREGATE_TYPE_P (TREE_TYPE (stmt)));
2077 : 34105335 : gcc_assert (!CONVERT_EXPR_VBASE_PATH (stmt));
2078 : : break;
2079 : :
2080 : 60712 : case SPACESHIP_EXPR:
2081 : 60712 : *stmt_p = genericize_spaceship (*stmt_p);
2082 : 60712 : break;
2083 : :
2084 : 32195 : case PTRMEM_CST:
2085 : : /* By the time we get here we're handing off to the back end, so we don't
2086 : : need or want to preserve PTRMEM_CST anymore. */
2087 : 32195 : *stmt_p = cplus_expand_constant (stmt);
2088 : 32195 : *walk_subtrees = 0;
2089 : 32195 : break;
2090 : :
2091 : 81541 : case MEM_REF:
2092 : : /* For MEM_REF, make sure not to sanitize the second operand even
2093 : : if it has reference type. It is just an offset with a type
2094 : : holding other information. There is no other processing we
2095 : : need to do for INTEGER_CSTs, so just ignore the second argument
2096 : : unconditionally. */
2097 : 81541 : cp_walk_tree (&TREE_OPERAND (stmt, 0), cp_genericize_r, data, NULL);
2098 : 81541 : *walk_subtrees = 0;
2099 : 81541 : break;
2100 : :
2101 : 90742729 : case NOP_EXPR:
2102 : 90742729 : *stmt_p = predeclare_vla (*stmt_p);
2103 : :
2104 : : /* Warn of new allocations that are not big enough for the target
2105 : : type. */
2106 : 90742729 : if (warn_alloc_size
2107 : 972163 : && TREE_CODE (TREE_OPERAND (stmt, 0)) == CALL_EXPR
2108 : 90802797 : && POINTER_TYPE_P (TREE_TYPE (stmt)))
2109 : : {
2110 : 20725 : if (tree fndecl = get_callee_fndecl (TREE_OPERAND (stmt, 0)))
2111 : 20711 : if (DECL_IS_MALLOC (fndecl))
2112 : : {
2113 : 1202 : tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (fndecl));
2114 : 1202 : tree alloc_size = lookup_attribute ("alloc_size", attrs);
2115 : 1202 : if (alloc_size)
2116 : 1200 : warn_for_alloc_size (EXPR_LOCATION (stmt),
2117 : 1200 : TREE_TYPE (TREE_TYPE (stmt)),
2118 : 1200 : TREE_OPERAND (stmt, 0), alloc_size);
2119 : : }
2120 : : }
2121 : :
2122 : 90742729 : if (!wtd->no_sanitize_p
2123 : 90742722 : && sanitize_flags_p (SANITIZE_NULL | SANITIZE_ALIGNMENT)
2124 : 90756324 : && TYPE_REF_P (TREE_TYPE (stmt)))
2125 : 1993 : ubsan_maybe_instrument_reference (stmt_p);
2126 : : break;
2127 : :
2128 : 59452968 : case CALL_EXPR:
2129 : 59452968 : if (!wtd->no_sanitize_p
2130 : 59452968 : && sanitize_flags_p ((SANITIZE_NULL
2131 : : | SANITIZE_ALIGNMENT | SANITIZE_VPTR)))
2132 : : {
2133 : 13723 : tree fn = CALL_EXPR_FN (stmt);
2134 : 13723 : if (fn != NULL_TREE
2135 : 8685 : && !error_operand_p (fn)
2136 : 8685 : && INDIRECT_TYPE_P (TREE_TYPE (fn))
2137 : 22408 : && TREE_CODE (TREE_TYPE (TREE_TYPE (fn))) == METHOD_TYPE)
2138 : : {
2139 : 4747 : bool is_ctor
2140 : 4747 : = TREE_CODE (fn) == ADDR_EXPR
2141 : 4626 : && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
2142 : 13999 : && DECL_CONSTRUCTOR_P (TREE_OPERAND (fn, 0));
2143 : 4747 : if (sanitize_flags_p (SANITIZE_NULL | SANITIZE_ALIGNMENT))
2144 : 4132 : ubsan_maybe_instrument_member_call (stmt, is_ctor);
2145 : 4747 : if (sanitize_flags_p (SANITIZE_VPTR) && !is_ctor)
2146 : 3998 : cp_ubsan_maybe_instrument_member_call (stmt);
2147 : : }
2148 : 8976 : else if (fn == NULL_TREE
2149 : 5038 : && CALL_EXPR_IFN (stmt) == IFN_UBSAN_NULL
2150 : 4104 : && TREE_CODE (CALL_EXPR_ARG (stmt, 0)) == INTEGER_CST
2151 : 8982 : && TYPE_REF_P (TREE_TYPE (CALL_EXPR_ARG (stmt, 0))))
2152 : 6 : *walk_subtrees = 0;
2153 : : }
2154 : : /* Fall through. */
2155 : 62987908 : case AGGR_INIT_EXPR:
2156 : : /* For calls to a multi-versioned function, overload resolution
2157 : : returns the function with the highest target priority, that is,
2158 : : the version that will checked for dispatching first. If this
2159 : : version is inlinable, a direct call to this version can be made
2160 : : otherwise the call should go through the dispatcher. */
2161 : 62987908 : {
2162 : 62987908 : tree fn = cp_get_callee_fndecl_nofold (stmt);
2163 : 61895849 : if (fn && DECL_FUNCTION_VERSIONED (fn)
2164 : 62988025 : && (current_function_decl == NULL
2165 : 117 : || !targetm.target_option.can_inline_p (current_function_decl,
2166 : : fn)))
2167 : 105 : if (tree dis = get_function_version_dispatcher (fn))
2168 : : {
2169 : 105 : mark_versions_used (dis);
2170 : 105 : dis = build_address (dis);
2171 : 105 : if (TREE_CODE (stmt) == CALL_EXPR)
2172 : 102 : CALL_EXPR_FN (stmt) = dis;
2173 : : else
2174 : 3 : AGGR_INIT_EXPR_FN (stmt) = dis;
2175 : : }
2176 : : }
2177 : : break;
2178 : :
2179 : 12541897 : case TARGET_EXPR:
2180 : 12541897 : if (TARGET_EXPR_INITIAL (stmt)
2181 : 12541897 : && TREE_CODE (TARGET_EXPR_INITIAL (stmt)) == CONSTRUCTOR
2182 : 14701028 : && CONSTRUCTOR_PLACEHOLDER_BOUNDARY (TARGET_EXPR_INITIAL (stmt)))
2183 : 166 : TARGET_EXPR_NO_ELIDE (stmt) = 1;
2184 : : break;
2185 : :
2186 : 80 : case TEMPLATE_ID_EXPR:
2187 : 80 : gcc_assert (concept_check_p (stmt));
2188 : : /* Emit the value of the concept check. */
2189 : 80 : *stmt_p = evaluate_concept_check (stmt);
2190 : 80 : walk_subtrees = 0;
2191 : 80 : break;
2192 : :
2193 : 4193 : case OMP_DISTRIBUTE:
2194 : : /* Need to explicitly instantiate copy ctors on class iterators of
2195 : : composite distribute parallel for. */
2196 : 4193 : if (OMP_FOR_INIT (*stmt_p) == NULL_TREE)
2197 : : {
2198 : 3718 : tree *data[4] = { NULL, NULL, NULL, NULL };
2199 : 3718 : tree inner = walk_tree (&OMP_FOR_BODY (*stmt_p),
2200 : : find_combined_omp_for, data, NULL);
2201 : 3718 : if (inner != NULL_TREE
2202 : 3684 : && TREE_CODE (inner) == OMP_FOR)
2203 : : {
2204 : 4502 : for (int i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (inner)); i++)
2205 : 2831 : if (TREE_VEC_ELT (OMP_FOR_INIT (inner), i)
2206 : 2815 : && OMP_FOR_ORIG_DECLS (inner)
2207 : 2815 : && TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner),
2208 : : i)) == TREE_LIST
2209 : 2855 : && TREE_PURPOSE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner),
2210 : : i)))
2211 : : {
2212 : 12 : tree orig = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner), i);
2213 : : /* Class iterators aren't allowed on OMP_SIMD, so the only
2214 : : case we need to solve is distribute parallel for. */
2215 : 12 : gcc_assert (TREE_CODE (inner) == OMP_FOR
2216 : : && data[1]);
2217 : 12 : tree orig_decl = TREE_PURPOSE (orig);
2218 : 12 : tree c, cl = NULL_TREE;
2219 : 12 : for (c = OMP_FOR_CLAUSES (inner);
2220 : 16 : c; c = OMP_CLAUSE_CHAIN (c))
2221 : 12 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
2222 : 5 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
2223 : 13 : && OMP_CLAUSE_DECL (c) == orig_decl)
2224 : : {
2225 : : cl = c;
2226 : : break;
2227 : : }
2228 : 12 : if (cl == NULL_TREE)
2229 : : {
2230 : 4 : for (c = OMP_PARALLEL_CLAUSES (*data[1]);
2231 : 4 : c; c = OMP_CLAUSE_CHAIN (c))
2232 : 1 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
2233 : 1 : && OMP_CLAUSE_DECL (c) == orig_decl)
2234 : : {
2235 : : cl = c;
2236 : : break;
2237 : : }
2238 : : }
2239 : 4 : if (cl)
2240 : : {
2241 : 9 : orig_decl = require_complete_type (orig_decl);
2242 : 9 : tree inner_type = TREE_TYPE (orig_decl);
2243 : 9 : if (orig_decl == error_mark_node)
2244 : 0 : continue;
2245 : 9 : if (TYPE_REF_P (TREE_TYPE (orig_decl)))
2246 : 0 : inner_type = TREE_TYPE (inner_type);
2247 : :
2248 : 9 : while (TREE_CODE (inner_type) == ARRAY_TYPE)
2249 : 0 : inner_type = TREE_TYPE (inner_type);
2250 : 9 : get_copy_ctor (inner_type, tf_warning_or_error);
2251 : : }
2252 : : }
2253 : : }
2254 : : }
2255 : : /* FALLTHRU */
2256 : :
2257 : 51009897 : case FOR_STMT:
2258 : 51009897 : case WHILE_STMT:
2259 : 51009897 : case DO_STMT:
2260 : 51009897 : case SWITCH_STMT:
2261 : 51009897 : case CONTINUE_STMT:
2262 : 51009897 : case BREAK_STMT:
2263 : 51009897 : case OMP_FOR:
2264 : 51009897 : case OMP_SIMD:
2265 : 51009897 : case OMP_LOOP:
2266 : 51009897 : case OMP_TILE:
2267 : 51009897 : case OMP_UNROLL:
2268 : 51009897 : case OACC_LOOP:
2269 : 51009897 : case STATEMENT_LIST:
2270 : : /* These cases are handled by shared code. */
2271 : 51009897 : c_genericize_control_stmt (stmt_p, walk_subtrees, data,
2272 : : cp_genericize_r, cp_walk_subtrees);
2273 : 51009897 : break;
2274 : :
2275 : 411 : case BIT_CAST_EXPR:
2276 : 411 : *stmt_p = build1_loc (EXPR_LOCATION (stmt), VIEW_CONVERT_EXPR,
2277 : 411 : TREE_TYPE (stmt), TREE_OPERAND (stmt, 0));
2278 : 411 : break;
2279 : :
2280 : 696804958 : default:
2281 : 696804958 : if (IS_TYPE_OR_DECL_P (stmt))
2282 : 201739712 : *walk_subtrees = 0;
2283 : : break;
2284 : : }
2285 : :
2286 : 1156315459 : p_set->add (*stmt_p);
2287 : :
2288 : 1156315459 : return NULL;
2289 : : }
2290 : :
2291 : : /* Lower C++ front end trees to GENERIC in T_P. */
2292 : :
2293 : : static void
2294 : 39384797 : cp_genericize_tree (tree* t_p, bool handle_invisiref_parm_p)
2295 : : {
2296 : 39384797 : struct cp_genericize_data wtd;
2297 : :
2298 : 39384797 : wtd.p_set = new hash_set<tree>;
2299 : 39384797 : wtd.bind_expr_stack.create (0);
2300 : 39384797 : wtd.omp_ctx = NULL;
2301 : 39384797 : wtd.try_block = NULL_TREE;
2302 : 39384797 : wtd.no_sanitize_p = false;
2303 : 39384797 : wtd.handle_invisiref_parm_p = handle_invisiref_parm_p;
2304 : 39384797 : cp_walk_tree (t_p, cp_genericize_r, &wtd, NULL);
2305 : 78769594 : delete wtd.p_set;
2306 : 39384797 : if (sanitize_flags_p (SANITIZE_VPTR))
2307 : 4891 : cp_ubsan_instrument_member_accesses (t_p);
2308 : 39384797 : }
2309 : :
2310 : : /* If a function that should end with a return in non-void
2311 : : function doesn't obviously end with return, add ubsan
2312 : : instrumentation code to verify it at runtime. If -fsanitize=return
2313 : : is not enabled, instrument __builtin_unreachable. */
2314 : :
2315 : : static void
2316 : 39384797 : cp_maybe_instrument_return (tree fndecl)
2317 : : {
2318 : 39384797 : if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fndecl)))
2319 : 56104112 : || DECL_CONSTRUCTOR_P (fndecl)
2320 : 28052056 : || DECL_DESTRUCTOR_P (fndecl)
2321 : 67436853 : || !targetm.warn_func_return (fndecl))
2322 : 11332753 : return;
2323 : :
2324 : 28052044 : if (!sanitize_flags_p (SANITIZE_RETURN, fndecl)
2325 : : /* Don't add __builtin_unreachable () if not optimizing, it will not
2326 : : improve any optimizations in that case, just break UB code.
2327 : : Don't add it if -fsanitize=unreachable -fno-sanitize=return either,
2328 : : UBSan covers this with ubsan_instrument_return above where sufficient
2329 : : information is provided, while the __builtin_unreachable () below
2330 : : if return sanitization is disabled will just result in hard to
2331 : : understand runtime error without location. */
2332 : 28052044 : && ((!optimize && !flag_unreachable_traps)
2333 : 28048795 : || sanitize_flags_p (SANITIZE_UNREACHABLE, fndecl)))
2334 : 30 : return;
2335 : :
2336 : 28052014 : tree t = DECL_SAVED_TREE (fndecl);
2337 : 50775877 : while (t)
2338 : : {
2339 : 50775877 : switch (TREE_CODE (t))
2340 : : {
2341 : 3904594 : case BIND_EXPR:
2342 : 3904594 : t = BIND_EXPR_BODY (t);
2343 : 3904594 : continue;
2344 : 7601254 : case TRY_FINALLY_EXPR:
2345 : 7601254 : case CLEANUP_POINT_EXPR:
2346 : 7601254 : t = TREE_OPERAND (t, 0);
2347 : 7601254 : continue;
2348 : 11220623 : case STATEMENT_LIST:
2349 : 11220623 : {
2350 : 11220623 : tree_stmt_iterator i = tsi_last (t);
2351 : 11225316 : while (!tsi_end_p (i))
2352 : : {
2353 : 11222708 : tree p = tsi_stmt (i);
2354 : 11222708 : if (TREE_CODE (p) != DEBUG_BEGIN_STMT)
2355 : : break;
2356 : 4693 : tsi_prev (&i);
2357 : : }
2358 : 11220623 : if (!tsi_end_p (i))
2359 : : {
2360 : 11218015 : t = tsi_stmt (i);
2361 : 11218015 : continue;
2362 : : }
2363 : : }
2364 : 2608 : break;
2365 : : case RETURN_EXPR:
2366 : : return;
2367 : : default:
2368 : : break;
2369 : 11505848 : }
2370 : : break;
2371 : : }
2372 : 13840520 : if (t == NULL_TREE)
2373 : : return;
2374 : 13840520 : tree *p = &DECL_SAVED_TREE (fndecl);
2375 : 13840520 : if (TREE_CODE (*p) == BIND_EXPR)
2376 : 912709 : p = &BIND_EXPR_BODY (*p);
2377 : :
2378 : 13840520 : location_t loc = DECL_SOURCE_LOCATION (fndecl);
2379 : 13840520 : if (sanitize_flags_p (SANITIZE_RETURN, fndecl))
2380 : 1317 : t = ubsan_instrument_return (loc);
2381 : : else
2382 : 13839203 : t = build_builtin_unreachable (BUILTINS_LOCATION);
2383 : :
2384 : 13840520 : append_to_statement_list (t, p);
2385 : : }
2386 : :
2387 : : void
2388 : 52441876 : cp_genericize (tree fndecl)
2389 : : {
2390 : 52441876 : tree t;
2391 : :
2392 : : /* Fix up the types of parms passed by invisible reference. */
2393 : 138352084 : for (t = DECL_ARGUMENTS (fndecl); t; t = DECL_CHAIN (t))
2394 : 85910208 : if (TREE_ADDRESSABLE (TREE_TYPE (t)))
2395 : : {
2396 : : /* If a function's arguments are copied to create a thunk,
2397 : : then DECL_BY_REFERENCE will be set -- but the type of the
2398 : : argument will be a pointer type, so we will never get
2399 : : here. */
2400 : 124900 : gcc_assert (!DECL_BY_REFERENCE (t));
2401 : 124900 : gcc_assert (DECL_ARG_TYPE (t) != TREE_TYPE (t));
2402 : 124900 : TREE_TYPE (t) = DECL_ARG_TYPE (t);
2403 : 124900 : DECL_BY_REFERENCE (t) = 1;
2404 : 124900 : TREE_ADDRESSABLE (t) = 0;
2405 : 124900 : relayout_decl (t);
2406 : : }
2407 : :
2408 : : /* Do the same for the return value. */
2409 : 52441876 : if (TREE_ADDRESSABLE (TREE_TYPE (DECL_RESULT (fndecl))))
2410 : : {
2411 : 990269 : t = DECL_RESULT (fndecl);
2412 : 990269 : TREE_TYPE (t) = build_reference_type (TREE_TYPE (t));
2413 : 990269 : DECL_BY_REFERENCE (t) = 1;
2414 : 990269 : TREE_ADDRESSABLE (t) = 0;
2415 : 990269 : relayout_decl (t);
2416 : 990269 : if (DECL_NAME (t))
2417 : : {
2418 : : /* Adjust DECL_VALUE_EXPR of the original var. */
2419 : 121574 : tree outer = outer_curly_brace_block (current_function_decl);
2420 : 121574 : tree var;
2421 : :
2422 : 121574 : if (outer)
2423 : 277909 : for (var = BLOCK_VARS (outer); var; var = DECL_CHAIN (var))
2424 : 277474 : if (VAR_P (var)
2425 : 267642 : && DECL_NAME (t) == DECL_NAME (var)
2426 : 121139 : && DECL_HAS_VALUE_EXPR_P (var)
2427 : 398613 : && DECL_VALUE_EXPR (var) == t)
2428 : : {
2429 : 121139 : tree val = convert_from_reference (t);
2430 : 121139 : SET_DECL_VALUE_EXPR (var, val);
2431 : 121139 : break;
2432 : : }
2433 : : }
2434 : : }
2435 : :
2436 : : /* If we're a clone, the body is already GIMPLE. */
2437 : 52441876 : if (DECL_CLONED_FUNCTION_P (fndecl))
2438 : 13057079 : return;
2439 : :
2440 : : /* Allow cp_genericize calls to be nested. */
2441 : 39384797 : bc_state_t save_state;
2442 : 39384797 : save_bc_state (&save_state);
2443 : :
2444 : : /* We do want to see every occurrence of the parms, so we can't just use
2445 : : walk_tree's hash functionality. */
2446 : 39384797 : cp_genericize_tree (&DECL_SAVED_TREE (fndecl), true);
2447 : :
2448 : 39384797 : cp_maybe_instrument_return (fndecl);
2449 : :
2450 : : /* Do everything else. */
2451 : 39384797 : c_genericize (fndecl);
2452 : 39384797 : restore_bc_state (&save_state);
2453 : : }
2454 : :
2455 : : /* Build code to apply FN to each member of ARG1 and ARG2. FN may be
2456 : : NULL if there is in fact nothing to do. ARG2 may be null if FN
2457 : : actually only takes one argument. */
2458 : :
2459 : : static tree
2460 : 3661 : cxx_omp_clause_apply_fn (tree fn, tree arg1, tree arg2)
2461 : : {
2462 : 3661 : tree defparm, parm, t;
2463 : 3661 : int i = 0;
2464 : 3661 : int nargs;
2465 : 3661 : tree *argarray;
2466 : :
2467 : 3661 : if (fn == NULL)
2468 : : return NULL;
2469 : :
2470 : 2827 : nargs = list_length (DECL_ARGUMENTS (fn));
2471 : 2827 : argarray = XALLOCAVEC (tree, nargs);
2472 : :
2473 : 2827 : defparm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
2474 : 2827 : if (arg2)
2475 : 944 : defparm = TREE_CHAIN (defparm);
2476 : :
2477 : 2827 : bool is_method = TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE;
2478 : 2827 : if (TREE_CODE (TREE_TYPE (arg1)) == ARRAY_TYPE)
2479 : : {
2480 : 27 : tree inner_type = TREE_TYPE (arg1);
2481 : 27 : tree start1, end1, p1;
2482 : 27 : tree start2 = NULL, p2 = NULL;
2483 : 27 : tree ret = NULL, lab;
2484 : :
2485 : 27 : start1 = arg1;
2486 : 27 : start2 = arg2;
2487 : 27 : do
2488 : : {
2489 : 27 : inner_type = TREE_TYPE (inner_type);
2490 : 27 : start1 = build4 (ARRAY_REF, inner_type, start1,
2491 : : size_zero_node, NULL, NULL);
2492 : 27 : if (arg2)
2493 : 9 : start2 = build4 (ARRAY_REF, inner_type, start2,
2494 : : size_zero_node, NULL, NULL);
2495 : : }
2496 : 27 : while (TREE_CODE (inner_type) == ARRAY_TYPE);
2497 : 27 : start1 = build_fold_addr_expr_loc (input_location, start1);
2498 : 27 : if (arg2)
2499 : 9 : start2 = build_fold_addr_expr_loc (input_location, start2);
2500 : :
2501 : 27 : end1 = TYPE_SIZE_UNIT (TREE_TYPE (arg1));
2502 : 27 : end1 = fold_build_pointer_plus (start1, end1);
2503 : :
2504 : 27 : p1 = create_tmp_var (TREE_TYPE (start1));
2505 : 27 : t = build2 (MODIFY_EXPR, TREE_TYPE (p1), p1, start1);
2506 : 27 : append_to_statement_list (t, &ret);
2507 : :
2508 : 27 : if (arg2)
2509 : : {
2510 : 9 : p2 = create_tmp_var (TREE_TYPE (start2));
2511 : 9 : t = build2 (MODIFY_EXPR, TREE_TYPE (p2), p2, start2);
2512 : 9 : append_to_statement_list (t, &ret);
2513 : : }
2514 : :
2515 : 27 : lab = create_artificial_label (input_location);
2516 : 27 : t = build1 (LABEL_EXPR, void_type_node, lab);
2517 : 27 : append_to_statement_list (t, &ret);
2518 : :
2519 : 27 : argarray[i++] = p1;
2520 : 27 : if (arg2)
2521 : 9 : argarray[i++] = p2;
2522 : : /* Handle default arguments. */
2523 : 27 : for (parm = defparm; parm && parm != void_list_node;
2524 : 0 : parm = TREE_CHAIN (parm), i++)
2525 : 0 : argarray[i] = convert_default_arg (TREE_VALUE (parm),
2526 : 0 : TREE_PURPOSE (parm), fn,
2527 : : i - is_method, tf_warning_or_error);
2528 : 27 : t = build_call_a (fn, i, argarray);
2529 : 27 : if (MAYBE_CLASS_TYPE_P (TREE_TYPE (t)))
2530 : 0 : t = build_cplus_new (TREE_TYPE (t), t, tf_warning_or_error);
2531 : 27 : t = fold_convert (void_type_node, t);
2532 : 27 : t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
2533 : 27 : append_to_statement_list (t, &ret);
2534 : :
2535 : 27 : t = fold_build_pointer_plus (p1, TYPE_SIZE_UNIT (inner_type));
2536 : 27 : t = build2 (MODIFY_EXPR, TREE_TYPE (p1), p1, t);
2537 : 27 : append_to_statement_list (t, &ret);
2538 : :
2539 : 27 : if (arg2)
2540 : : {
2541 : 9 : t = fold_build_pointer_plus (p2, TYPE_SIZE_UNIT (inner_type));
2542 : 9 : t = build2 (MODIFY_EXPR, TREE_TYPE (p2), p2, t);
2543 : 9 : append_to_statement_list (t, &ret);
2544 : : }
2545 : :
2546 : 27 : t = build2 (NE_EXPR, boolean_type_node, p1, end1);
2547 : 27 : t = build3 (COND_EXPR, void_type_node, t, build_and_jump (&lab), NULL);
2548 : 27 : append_to_statement_list (t, &ret);
2549 : :
2550 : 27 : return ret;
2551 : : }
2552 : : else
2553 : : {
2554 : 2800 : argarray[i++] = build_fold_addr_expr_loc (input_location, arg1);
2555 : 2800 : if (arg2)
2556 : 935 : argarray[i++] = build_fold_addr_expr_loc (input_location, arg2);
2557 : : /* Handle default arguments. */
2558 : 2805 : for (parm = defparm; parm && parm != void_list_node;
2559 : 5 : parm = TREE_CHAIN (parm), i++)
2560 : 10 : argarray[i] = convert_default_arg (TREE_VALUE (parm),
2561 : 5 : TREE_PURPOSE (parm), fn,
2562 : : i - is_method, tf_warning_or_error);
2563 : 2800 : t = build_call_a (fn, i, argarray);
2564 : 2800 : if (MAYBE_CLASS_TYPE_P (TREE_TYPE (t)))
2565 : 1 : t = build_cplus_new (TREE_TYPE (t), t, tf_warning_or_error);
2566 : 2800 : t = fold_convert (void_type_node, t);
2567 : 2800 : return fold_build_cleanup_point_expr (TREE_TYPE (t), t);
2568 : : }
2569 : : }
2570 : :
2571 : : /* Return code to initialize DECL with its default constructor, or
2572 : : NULL if there's nothing to do. */
2573 : :
2574 : : tree
2575 : 42447 : cxx_omp_clause_default_ctor (tree clause, tree decl, tree /*outer*/)
2576 : : {
2577 : 42447 : tree info = CP_OMP_CLAUSE_INFO (clause);
2578 : 42447 : tree ret = NULL;
2579 : :
2580 : 42447 : if (info)
2581 : 1389 : ret = cxx_omp_clause_apply_fn (TREE_VEC_ELT (info, 0), decl, NULL);
2582 : :
2583 : 42447 : return ret;
2584 : : }
2585 : :
2586 : : /* Return code to initialize DST with a copy constructor from SRC. */
2587 : :
2588 : : tree
2589 : 12387 : cxx_omp_clause_copy_ctor (tree clause, tree dst, tree src)
2590 : : {
2591 : 12387 : tree info = CP_OMP_CLAUSE_INFO (clause);
2592 : 12387 : tree ret = NULL;
2593 : :
2594 : 12387 : if (info)
2595 : 285 : ret = cxx_omp_clause_apply_fn (TREE_VEC_ELT (info, 0), dst, src);
2596 : 285 : if (ret == NULL)
2597 : 12169 : ret = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
2598 : :
2599 : 12387 : return ret;
2600 : : }
2601 : :
2602 : : /* Similarly, except use an assignment operator instead. */
2603 : :
2604 : : tree
2605 : 12679 : cxx_omp_clause_assign_op (tree clause, tree dst, tree src)
2606 : : {
2607 : 12679 : tree info = CP_OMP_CLAUSE_INFO (clause);
2608 : 12679 : tree ret = NULL;
2609 : :
2610 : 12679 : if (info)
2611 : 748 : ret = cxx_omp_clause_apply_fn (TREE_VEC_ELT (info, 2), dst, src);
2612 : 748 : if (ret == NULL)
2613 : 11953 : ret = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
2614 : :
2615 : 12679 : return ret;
2616 : : }
2617 : :
2618 : : /* Return code to destroy DECL. */
2619 : :
2620 : : tree
2621 : 62383 : cxx_omp_clause_dtor (tree clause, tree decl)
2622 : : {
2623 : 62383 : tree info = CP_OMP_CLAUSE_INFO (clause);
2624 : 62383 : tree ret = NULL;
2625 : :
2626 : 62383 : if (info)
2627 : 1239 : ret = cxx_omp_clause_apply_fn (TREE_VEC_ELT (info, 1), decl, NULL);
2628 : :
2629 : 62383 : return ret;
2630 : : }
2631 : :
2632 : : /* True if OpenMP should privatize what this DECL points to rather
2633 : : than the DECL itself. */
2634 : :
2635 : : bool
2636 : 889970 : cxx_omp_privatize_by_reference (const_tree decl)
2637 : : {
2638 : 889970 : return (TYPE_REF_P (TREE_TYPE (decl))
2639 : 889970 : || is_invisiref_parm (decl));
2640 : : }
2641 : :
2642 : : /* Return true if DECL is const qualified var having no mutable member. */
2643 : : bool
2644 : 15258 : cxx_omp_const_qual_no_mutable (tree decl)
2645 : : {
2646 : 15258 : tree type = TREE_TYPE (decl);
2647 : 15258 : if (TYPE_REF_P (type))
2648 : : {
2649 : 847 : if (!is_invisiref_parm (decl))
2650 : : return false;
2651 : 0 : type = TREE_TYPE (type);
2652 : :
2653 : 0 : if (TREE_CODE (decl) == RESULT_DECL && DECL_NAME (decl))
2654 : : {
2655 : : /* NVR doesn't preserve const qualification of the
2656 : : variable's type. */
2657 : 0 : tree outer = outer_curly_brace_block (current_function_decl);
2658 : 0 : tree var;
2659 : :
2660 : 0 : if (outer)
2661 : 0 : for (var = BLOCK_VARS (outer); var; var = DECL_CHAIN (var))
2662 : 0 : if (VAR_P (var)
2663 : 0 : && DECL_NAME (decl) == DECL_NAME (var)
2664 : 0 : && (TYPE_MAIN_VARIANT (type)
2665 : 0 : == TYPE_MAIN_VARIANT (TREE_TYPE (var))))
2666 : : {
2667 : 0 : if (TYPE_READONLY (TREE_TYPE (var)))
2668 : 0 : type = TREE_TYPE (var);
2669 : : break;
2670 : : }
2671 : : }
2672 : : }
2673 : :
2674 : 14411 : if (type == error_mark_node)
2675 : : return false;
2676 : :
2677 : : /* Variables with const-qualified type having no mutable member
2678 : : are predetermined shared. */
2679 : 14396 : if (TYPE_READONLY (type) && !cp_has_mutable_p (type))
2680 : : return true;
2681 : :
2682 : : return false;
2683 : : }
2684 : :
2685 : : /* OMP_CLAUSE_DEFAULT_UNSPECIFIED unless OpenMP sharing attribute
2686 : : of DECL is predetermined. */
2687 : :
2688 : : enum omp_clause_default_kind
2689 : 54986 : cxx_omp_predetermined_sharing_1 (tree decl)
2690 : : {
2691 : : /* Static data members are predetermined shared. */
2692 : 54986 : if (TREE_STATIC (decl))
2693 : : {
2694 : 15101 : tree ctx = CP_DECL_CONTEXT (decl);
2695 : 15101 : if (TYPE_P (ctx) && MAYBE_CLASS_TYPE_P (ctx))
2696 : : return OMP_CLAUSE_DEFAULT_SHARED;
2697 : :
2698 : 14995 : if (c_omp_predefined_variable (decl))
2699 : : return OMP_CLAUSE_DEFAULT_SHARED;
2700 : : }
2701 : :
2702 : : /* this may not be specified in data-sharing clauses, still we need
2703 : : to predetermined it firstprivate. */
2704 : 54835 : if (decl == current_class_ptr)
2705 : 110 : return OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
2706 : :
2707 : : return OMP_CLAUSE_DEFAULT_UNSPECIFIED;
2708 : : }
2709 : :
2710 : : /* Likewise, but also include the artificial vars. We don't want to
2711 : : disallow the artificial vars being mentioned in explicit clauses,
2712 : : as we use artificial vars e.g. for loop constructs with random
2713 : : access iterators other than pointers, but during gimplification
2714 : : we want to treat them as predetermined. */
2715 : :
2716 : : enum omp_clause_default_kind
2717 : 34575 : cxx_omp_predetermined_sharing (tree decl)
2718 : : {
2719 : 34575 : enum omp_clause_default_kind ret = cxx_omp_predetermined_sharing_1 (decl);
2720 : 34575 : if (ret != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
2721 : : return ret;
2722 : :
2723 : : /* Predetermine artificial variables holding integral values, those
2724 : : are usually result of gimplify_one_sizepos or SAVE_EXPR
2725 : : gimplification. */
2726 : 34361 : if (VAR_P (decl)
2727 : 22490 : && DECL_ARTIFICIAL (decl)
2728 : 6987 : && INTEGRAL_TYPE_P (TREE_TYPE (decl))
2729 : 34879 : && !(DECL_LANG_SPECIFIC (decl)
2730 : 2 : && DECL_OMP_PRIVATIZED_MEMBER (decl)))
2731 : : return OMP_CLAUSE_DEFAULT_SHARED;
2732 : :
2733 : : /* Similarly for typeinfo symbols. */
2734 : 33845 : if (VAR_P (decl) && DECL_ARTIFICIAL (decl) && DECL_TINFO_P (decl))
2735 : 57 : return OMP_CLAUSE_DEFAULT_SHARED;
2736 : :
2737 : : return OMP_CLAUSE_DEFAULT_UNSPECIFIED;
2738 : : }
2739 : :
2740 : : enum omp_clause_defaultmap_kind
2741 : 6388 : cxx_omp_predetermined_mapping (tree decl)
2742 : : {
2743 : : /* Predetermine artificial variables holding integral values, those
2744 : : are usually result of gimplify_one_sizepos or SAVE_EXPR
2745 : : gimplification. */
2746 : 6388 : if (VAR_P (decl)
2747 : 1297 : && DECL_ARTIFICIAL (decl)
2748 : 81 : && INTEGRAL_TYPE_P (TREE_TYPE (decl))
2749 : 6454 : && !(DECL_LANG_SPECIFIC (decl)
2750 : 6 : && DECL_OMP_PRIVATIZED_MEMBER (decl)))
2751 : : return OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
2752 : :
2753 : 6322 : if (c_omp_predefined_variable (decl))
2754 : 0 : return OMP_CLAUSE_DEFAULTMAP_TO;
2755 : :
2756 : : return OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
2757 : : }
2758 : :
2759 : : /* Finalize an implicitly determined clause. */
2760 : :
2761 : : void
2762 : 63828 : cxx_omp_finish_clause (tree c, gimple_seq *, bool /* openacc */)
2763 : : {
2764 : 63828 : tree decl, inner_type;
2765 : 63828 : bool make_shared = false;
2766 : :
2767 : 63828 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE
2768 : 56455 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_PRIVATE
2769 : 93653 : && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_LASTPRIVATE
2770 : 4753 : || !OMP_CLAUSE_LASTPRIVATE_LOOP_IV (c)))
2771 : : return;
2772 : :
2773 : 34018 : decl = OMP_CLAUSE_DECL (c);
2774 : 34018 : decl = require_complete_type (decl);
2775 : 34018 : inner_type = TREE_TYPE (decl);
2776 : 34018 : if (decl == error_mark_node)
2777 : 34018 : make_shared = true;
2778 : 34018 : else if (TYPE_REF_P (TREE_TYPE (decl)))
2779 : 89 : inner_type = TREE_TYPE (inner_type);
2780 : :
2781 : : /* We're interested in the base element, not arrays. */
2782 : 34271 : while (TREE_CODE (inner_type) == ARRAY_TYPE)
2783 : 253 : inner_type = TREE_TYPE (inner_type);
2784 : :
2785 : : /* Check for special function availability by building a call to one.
2786 : : Save the results, because later we won't be in the right context
2787 : : for making these queries. */
2788 : 34018 : bool first = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE;
2789 : 34018 : bool last = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE;
2790 : 34018 : if (!make_shared
2791 : 34018 : && CLASS_TYPE_P (inner_type)
2792 : 34242 : && cxx_omp_create_clause_info (c, inner_type, !first, first, last,
2793 : : true))
2794 : : make_shared = true;
2795 : :
2796 : 34012 : if (make_shared)
2797 : : {
2798 : 6 : OMP_CLAUSE_CODE (c) = OMP_CLAUSE_SHARED;
2799 : 6 : OMP_CLAUSE_SHARED_FIRSTPRIVATE (c) = 0;
2800 : 6 : OMP_CLAUSE_SHARED_READONLY (c) = 0;
2801 : : }
2802 : : }
2803 : :
2804 : : /* Return true if DECL's DECL_VALUE_EXPR (if any) should be
2805 : : disregarded in OpenMP construct, because it is going to be
2806 : : remapped during OpenMP lowering. SHARED is true if DECL
2807 : : is going to be shared, false if it is going to be privatized. */
2808 : :
2809 : : bool
2810 : 570943 : cxx_omp_disregard_value_expr (tree decl, bool shared)
2811 : : {
2812 : 570943 : if (shared)
2813 : : return false;
2814 : 400330 : if (VAR_P (decl)
2815 : 378373 : && DECL_HAS_VALUE_EXPR_P (decl)
2816 : 9349 : && DECL_ARTIFICIAL (decl)
2817 : 8849 : && DECL_LANG_SPECIFIC (decl)
2818 : 408133 : && DECL_OMP_PRIVATIZED_MEMBER (decl))
2819 : : return true;
2820 : 394992 : if (VAR_P (decl) && DECL_CONTEXT (decl) && is_capture_proxy (decl))
2821 : : return true;
2822 : : return false;
2823 : : }
2824 : :
2825 : : /* Fold expression X which is used as an rvalue if RVAL is true. */
2826 : :
2827 : : static tree
2828 : 1424732134 : cp_fold_maybe_rvalue (tree x, bool rval, fold_flags_t flags)
2829 : : {
2830 : 1450458302 : while (true)
2831 : : {
2832 : 1437595218 : x = cp_fold (x, flags);
2833 : 1437595218 : if (rval)
2834 : 1031603018 : x = mark_rvalue_use (x);
2835 : 1031603018 : if (rval && DECL_P (x)
2836 : 263952408 : && !TYPE_REF_P (TREE_TYPE (x)))
2837 : : {
2838 : 229186199 : tree v = decl_constant_value (x);
2839 : 229186199 : if (v != x && v != error_mark_node)
2840 : : {
2841 : 12863084 : x = v;
2842 : 12863084 : continue;
2843 : : }
2844 : : }
2845 : 1424732134 : break;
2846 : 12863084 : }
2847 : 1424732134 : return x;
2848 : : }
2849 : :
2850 : : tree
2851 : 58719990 : cp_fold_maybe_rvalue (tree x, bool rval)
2852 : : {
2853 : 58719990 : return cp_fold_maybe_rvalue (x, rval, ff_none);
2854 : : }
2855 : :
2856 : : /* Fold expression X which is used as an rvalue. */
2857 : :
2858 : : static tree
2859 : 472405364 : cp_fold_rvalue (tree x, fold_flags_t flags)
2860 : : {
2861 : 296073426 : return cp_fold_maybe_rvalue (x, true, flags);
2862 : : }
2863 : :
2864 : : tree
2865 : 301162 : cp_fold_rvalue (tree x)
2866 : : {
2867 : 301162 : return cp_fold_rvalue (x, ff_none);
2868 : : }
2869 : :
2870 : : /* Perform folding on expression X. */
2871 : :
2872 : : static tree
2873 : 193089098 : cp_fully_fold (tree x, mce_value manifestly_const_eval)
2874 : : {
2875 : 193089098 : if (processing_template_decl)
2876 : : return x;
2877 : : /* FIXME cp_fold ought to be a superset of maybe_constant_value so we don't
2878 : : have to call both. */
2879 : 176030776 : if (cxx_dialect >= cxx11)
2880 : : {
2881 : 175015377 : x = maybe_constant_value (x, /*decl=*/NULL_TREE, manifestly_const_eval);
2882 : : /* Sometimes we are given a CONSTRUCTOR but the call above wraps it into
2883 : : a TARGET_EXPR; undo that here. */
2884 : 175015377 : if (TREE_CODE (x) == TARGET_EXPR)
2885 : 611438 : x = TARGET_EXPR_INITIAL (x);
2886 : 174403939 : else if (TREE_CODE (x) == VIEW_CONVERT_EXPR
2887 : 24570785 : && TREE_CODE (TREE_OPERAND (x, 0)) == CONSTRUCTOR
2888 : 174404126 : && TREE_TYPE (TREE_OPERAND (x, 0)) == TREE_TYPE (x))
2889 : 187 : x = TREE_OPERAND (x, 0);
2890 : : }
2891 : 176030776 : fold_flags_t flags = ff_none;
2892 : 176030776 : if (manifestly_const_eval == mce_false)
2893 : 29410081 : flags |= ff_mce_false;
2894 : 176030776 : return cp_fold_rvalue (x, flags);
2895 : : }
2896 : :
2897 : : tree
2898 : 163679017 : cp_fully_fold (tree x)
2899 : : {
2900 : 163679017 : return cp_fully_fold (x, mce_unknown);
2901 : : }
2902 : :
2903 : : /* Likewise, but also fold recursively, which cp_fully_fold doesn't perform
2904 : : in some cases. */
2905 : :
2906 : : tree
2907 : 30950130 : cp_fully_fold_init (tree x)
2908 : : {
2909 : 30950130 : if (processing_template_decl)
2910 : 1540049 : return x;
2911 : 29410081 : x = cp_fully_fold (x, mce_false);
2912 : 29410081 : cp_fold_data data (ff_mce_false);
2913 : 29410081 : cp_walk_tree (&x, cp_fold_r, &data, NULL);
2914 : 29410081 : return x;
2915 : 29410081 : }
2916 : :
2917 : : /* c-common interface to cp_fold. If IN_INIT, this is in a static initializer
2918 : : and certain changes are made to the folding done. Or should be (FIXME). We
2919 : : never touch maybe_const, as it is only used for the C front-end
2920 : : C_MAYBE_CONST_EXPR. */
2921 : :
2922 : : tree
2923 : 58719990 : c_fully_fold (tree x, bool /*in_init*/, bool */*maybe_const*/, bool lval)
2924 : : {
2925 : 58719990 : return cp_fold_maybe_rvalue (x, !lval);
2926 : : }
2927 : :
2928 : : static GTY((deletable)) hash_map<tree, tree> *fold_caches[2];
2929 : :
2930 : : /* Subroutine of cp_fold. Returns which fold cache to use according
2931 : : to the given flags. We need multiple caches since the result of
2932 : : folding may depend on which flags are used. */
2933 : :
2934 : : static hash_map<tree, tree> *&
2935 : 2107683669 : get_fold_cache (fold_flags_t flags)
2936 : : {
2937 : 0 : if (flags & ff_mce_false)
2938 : 1801495867 : return fold_caches[1];
2939 : : else
2940 : 306187802 : return fold_caches[0];
2941 : : }
2942 : :
2943 : : /* Dispose of the whole FOLD_CACHE. */
2944 : :
2945 : : void
2946 : 22576771 : clear_fold_cache (void)
2947 : : {
2948 : 67730313 : for (auto& fold_cache : fold_caches)
2949 : 45153542 : if (fold_cache != NULL)
2950 : 55752742 : fold_cache->empty ();
2951 : 22576771 : }
2952 : :
2953 : : /* This function tries to fold an expression X.
2954 : : To avoid combinatorial explosion, folding results are kept in fold_cache.
2955 : : If X is invalid, we don't fold at all.
2956 : : For performance reasons we don't cache expressions representing a
2957 : : declaration or constant.
2958 : : Function returns X or its folded variant. */
2959 : :
2960 : : static tree
2961 : 3426195056 : cp_fold (tree x, fold_flags_t flags)
2962 : : {
2963 : 3426195056 : tree op0, op1, op2, op3;
2964 : 3426195056 : tree org_x = x, r = NULL_TREE;
2965 : 3426195056 : enum tree_code code;
2966 : 3426195056 : location_t loc;
2967 : 3426195056 : bool rval_ops = true;
2968 : :
2969 : 3426195056 : if (!x || x == error_mark_node)
2970 : : return x;
2971 : :
2972 : 3422867379 : if (EXPR_P (x) && (!TREE_TYPE (x) || TREE_TYPE (x) == error_mark_node))
2973 : : return x;
2974 : :
2975 : : /* Don't bother to cache DECLs or constants. */
2976 : 3422738848 : if (DECL_P (x) || CONSTANT_CLASS_P (x))
2977 : : return x;
2978 : :
2979 : 2107683669 : auto& fold_cache = get_fold_cache (flags);
2980 : 2107683669 : if (fold_cache == NULL)
2981 : 300643 : fold_cache = hash_map<tree, tree>::create_ggc (101);
2982 : :
2983 : 2107683669 : if (tree *cached = fold_cache->get (x))
2984 : : {
2985 : : /* unshare_expr doesn't recurse into SAVE_EXPRs. If SAVE_EXPR's
2986 : : argument has been folded into a tree invariant, make sure it is
2987 : : unshared. See PR112727. */
2988 : 423553095 : if (TREE_CODE (x) == SAVE_EXPR && *cached != x)
2989 : 77 : return unshare_expr (*cached);
2990 : 423553018 : return *cached;
2991 : : }
2992 : :
2993 : 1684130574 : uid_sensitive_constexpr_evaluation_checker c;
2994 : :
2995 : 1684130574 : code = TREE_CODE (x);
2996 : 1684130574 : switch (code)
2997 : : {
2998 : 90997157 : case CLEANUP_POINT_EXPR:
2999 : : /* Strip CLEANUP_POINT_EXPR if the expression doesn't have side
3000 : : effects. */
3001 : 90997157 : r = cp_fold_rvalue (TREE_OPERAND (x, 0), flags);
3002 : 90997157 : if (!TREE_SIDE_EFFECTS (r))
3003 : 1274750 : x = r;
3004 : : break;
3005 : :
3006 : 1364746 : case SIZEOF_EXPR:
3007 : 1364746 : x = fold_sizeof_expr (x);
3008 : 1364746 : break;
3009 : :
3010 : 195143858 : case VIEW_CONVERT_EXPR:
3011 : 195143858 : rval_ops = false;
3012 : : /* FALLTHRU */
3013 : 522030894 : case NON_LVALUE_EXPR:
3014 : 522030894 : CASE_CONVERT:
3015 : :
3016 : 522030894 : if (VOID_TYPE_P (TREE_TYPE (x)))
3017 : : {
3018 : : /* This is just to make sure we don't end up with casts to
3019 : : void from error_mark_node. If we just return x, then
3020 : : cp_fold_r might fold the operand into error_mark_node and
3021 : : leave the conversion in the IR. STRIP_USELESS_TYPE_CONVERSION
3022 : : during gimplification doesn't like such casts.
3023 : : Don't create a new tree if op0 != TREE_OPERAND (x, 0), the
3024 : : folding of the operand should be in the caches and if in cp_fold_r
3025 : : it will modify it in place. */
3026 : 37172126 : op0 = cp_fold (TREE_OPERAND (x, 0), flags);
3027 : 37172126 : if (op0 == error_mark_node)
3028 : 53 : x = error_mark_node;
3029 : : break;
3030 : : }
3031 : :
3032 : 484858768 : loc = EXPR_LOCATION (x);
3033 : 484858768 : op0 = cp_fold_maybe_rvalue (TREE_OPERAND (x, 0), rval_ops, flags);
3034 : :
3035 : 484858768 : if (op0 == error_mark_node)
3036 : 0 : x = error_mark_node;
3037 : 484858768 : else if (code == CONVERT_EXPR
3038 : 36018786 : && SCALAR_TYPE_P (TREE_TYPE (x))
3039 : 520876736 : && op0 != void_node)
3040 : : /* During parsing we used convert_to_*_nofold; re-convert now using the
3041 : : folding variants, since fold() doesn't do those transformations. */
3042 : 32553587 : x = fold (convert (TREE_TYPE (x), op0));
3043 : 452305181 : else if (op0 != TREE_OPERAND (x, 0))
3044 : 121895917 : x = fold_build1_loc (loc, code, TREE_TYPE (x), op0);
3045 : : else
3046 : 330409264 : x = fold (x);
3047 : :
3048 : : /* Conversion of an out-of-range value has implementation-defined
3049 : : behavior; the language considers it different from arithmetic
3050 : : overflow, which is undefined. */
3051 : 484858768 : if (TREE_CODE (op0) == INTEGER_CST
3052 : 484858768 : && TREE_OVERFLOW_P (x) && !TREE_OVERFLOW_P (op0))
3053 : 44 : TREE_OVERFLOW (x) = false;
3054 : :
3055 : : break;
3056 : :
3057 : 180 : case EXCESS_PRECISION_EXPR:
3058 : 180 : op0 = cp_fold_maybe_rvalue (TREE_OPERAND (x, 0), rval_ops, flags);
3059 : 180 : x = fold_convert_loc (EXPR_LOCATION (x), TREE_TYPE (x), op0);
3060 : 180 : break;
3061 : :
3062 : 56409908 : case INDIRECT_REF:
3063 : : /* We don't need the decltype(auto) obfuscation anymore. */
3064 : 56409908 : if (REF_PARENTHESIZED_P (x))
3065 : : {
3066 : 476 : tree p = maybe_undo_parenthesized_ref (x);
3067 : 476 : if (p != x)
3068 : 0 : return cp_fold (p, flags);
3069 : : }
3070 : 56409908 : goto unary;
3071 : :
3072 : 123505726 : case ADDR_EXPR:
3073 : 123505726 : loc = EXPR_LOCATION (x);
3074 : 123505726 : op0 = cp_fold_maybe_rvalue (TREE_OPERAND (x, 0), false, flags);
3075 : :
3076 : : /* Cope with user tricks that amount to offsetof. */
3077 : 123505726 : if (op0 != error_mark_node
3078 : 123505726 : && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (op0)))
3079 : : {
3080 : 48130308 : tree val = get_base_address (op0);
3081 : 48130308 : if (val
3082 : 48130308 : && INDIRECT_REF_P (val)
3083 : 19741914 : && COMPLETE_TYPE_P (TREE_TYPE (val))
3084 : 67872132 : && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3085 : : {
3086 : 252 : val = TREE_OPERAND (val, 0);
3087 : 252 : STRIP_NOPS (val);
3088 : 252 : val = maybe_constant_value (val);
3089 : 252 : if (TREE_CODE (val) == INTEGER_CST)
3090 : 129 : return fold_offsetof (op0, TREE_TYPE (x));
3091 : : }
3092 : : }
3093 : 123505597 : goto finish_unary;
3094 : :
3095 : : case REALPART_EXPR:
3096 : : case IMAGPART_EXPR:
3097 : 84771992 : rval_ops = false;
3098 : : /* FALLTHRU */
3099 : 84771992 : case CONJ_EXPR:
3100 : 84771992 : case FIX_TRUNC_EXPR:
3101 : 84771992 : case FLOAT_EXPR:
3102 : 84771992 : case NEGATE_EXPR:
3103 : 84771992 : case ABS_EXPR:
3104 : 84771992 : case ABSU_EXPR:
3105 : 84771992 : case BIT_NOT_EXPR:
3106 : 84771992 : case TRUTH_NOT_EXPR:
3107 : 84771992 : case FIXED_CONVERT_EXPR:
3108 : 84771992 : unary:
3109 : :
3110 : 84771992 : loc = EXPR_LOCATION (x);
3111 : 84771992 : op0 = cp_fold_maybe_rvalue (TREE_OPERAND (x, 0), rval_ops, flags);
3112 : :
3113 : 208277589 : finish_unary:
3114 : 208277589 : if (op0 == error_mark_node)
3115 : 0 : x = error_mark_node;
3116 : 208277589 : else if (op0 != TREE_OPERAND (x, 0))
3117 : : {
3118 : 28750910 : x = fold_build1_loc (loc, code, TREE_TYPE (x), op0);
3119 : 28750910 : if (code == INDIRECT_REF
3120 : 6867762 : && (INDIRECT_REF_P (x) || TREE_CODE (x) == MEM_REF))
3121 : : {
3122 : 6867638 : TREE_READONLY (x) = TREE_READONLY (org_x);
3123 : 6867638 : TREE_SIDE_EFFECTS (x) = TREE_SIDE_EFFECTS (org_x);
3124 : 6867638 : TREE_THIS_VOLATILE (x) = TREE_THIS_VOLATILE (org_x);
3125 : : }
3126 : : }
3127 : : else
3128 : 179526679 : x = fold (x);
3129 : :
3130 : 208277589 : gcc_assert (TREE_CODE (x) != COND_EXPR
3131 : : || !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))));
3132 : : break;
3133 : :
3134 : 193145 : case UNARY_PLUS_EXPR:
3135 : 193145 : op0 = cp_fold_rvalue (TREE_OPERAND (x, 0), flags);
3136 : 193145 : if (op0 == error_mark_node)
3137 : 0 : x = error_mark_node;
3138 : : else
3139 : 193145 : x = fold_convert (TREE_TYPE (x), op0);
3140 : : break;
3141 : :
3142 : 85560048 : case POSTDECREMENT_EXPR:
3143 : 85560048 : case POSTINCREMENT_EXPR:
3144 : 85560048 : case INIT_EXPR:
3145 : 85560048 : case PREDECREMENT_EXPR:
3146 : 85560048 : case PREINCREMENT_EXPR:
3147 : 85560048 : case COMPOUND_EXPR:
3148 : 85560048 : case MODIFY_EXPR:
3149 : 85560048 : rval_ops = false;
3150 : : /* FALLTHRU */
3151 : 200470114 : case POINTER_PLUS_EXPR:
3152 : 200470114 : case PLUS_EXPR:
3153 : 200470114 : case POINTER_DIFF_EXPR:
3154 : 200470114 : case MINUS_EXPR:
3155 : 200470114 : case MULT_EXPR:
3156 : 200470114 : case TRUNC_DIV_EXPR:
3157 : 200470114 : case CEIL_DIV_EXPR:
3158 : 200470114 : case FLOOR_DIV_EXPR:
3159 : 200470114 : case ROUND_DIV_EXPR:
3160 : 200470114 : case TRUNC_MOD_EXPR:
3161 : 200470114 : case CEIL_MOD_EXPR:
3162 : 200470114 : case ROUND_MOD_EXPR:
3163 : 200470114 : case RDIV_EXPR:
3164 : 200470114 : case EXACT_DIV_EXPR:
3165 : 200470114 : case MIN_EXPR:
3166 : 200470114 : case MAX_EXPR:
3167 : 200470114 : case LSHIFT_EXPR:
3168 : 200470114 : case RSHIFT_EXPR:
3169 : 200470114 : case LROTATE_EXPR:
3170 : 200470114 : case RROTATE_EXPR:
3171 : 200470114 : case BIT_AND_EXPR:
3172 : 200470114 : case BIT_IOR_EXPR:
3173 : 200470114 : case BIT_XOR_EXPR:
3174 : 200470114 : case TRUTH_AND_EXPR:
3175 : 200470114 : case TRUTH_ANDIF_EXPR:
3176 : 200470114 : case TRUTH_OR_EXPR:
3177 : 200470114 : case TRUTH_ORIF_EXPR:
3178 : 200470114 : case TRUTH_XOR_EXPR:
3179 : 200470114 : case LT_EXPR: case LE_EXPR:
3180 : 200470114 : case GT_EXPR: case GE_EXPR:
3181 : 200470114 : case EQ_EXPR: case NE_EXPR:
3182 : 200470114 : case UNORDERED_EXPR: case ORDERED_EXPR:
3183 : 200470114 : case UNLT_EXPR: case UNLE_EXPR:
3184 : 200470114 : case UNGT_EXPR: case UNGE_EXPR:
3185 : 200470114 : case UNEQ_EXPR: case LTGT_EXPR:
3186 : 200470114 : case RANGE_EXPR: case COMPLEX_EXPR:
3187 : :
3188 : 200470114 : loc = EXPR_LOCATION (x);
3189 : 200470114 : op0 = cp_fold_maybe_rvalue (TREE_OPERAND (x, 0), rval_ops, flags);
3190 : 200470114 : op1 = cp_fold_rvalue (TREE_OPERAND (x, 1), flags);
3191 : :
3192 : : /* decltype(nullptr) has only one value, so optimize away all comparisons
3193 : : with that type right away, keeping them in the IL causes troubles for
3194 : : various optimizations. */
3195 : 200470114 : if (COMPARISON_CLASS_P (org_x)
3196 : 30925609 : && TREE_CODE (TREE_TYPE (op0)) == NULLPTR_TYPE
3197 : 200470144 : && TREE_CODE (TREE_TYPE (op1)) == NULLPTR_TYPE)
3198 : : {
3199 : 30 : switch (code)
3200 : : {
3201 : 15 : case EQ_EXPR:
3202 : 15 : x = constant_boolean_node (true, TREE_TYPE (x));
3203 : 15 : break;
3204 : 15 : case NE_EXPR:
3205 : 15 : x = constant_boolean_node (false, TREE_TYPE (x));
3206 : 15 : break;
3207 : 0 : default:
3208 : 0 : gcc_unreachable ();
3209 : : }
3210 : 30 : return omit_two_operands_loc (loc, TREE_TYPE (x), x,
3211 : 30 : op0, op1);
3212 : : }
3213 : :
3214 : 200470084 : if (op0 == error_mark_node || op1 == error_mark_node)
3215 : 81 : x = error_mark_node;
3216 : 200470003 : else if (op0 != TREE_OPERAND (x, 0) || op1 != TREE_OPERAND (x, 1))
3217 : 138007242 : x = fold_build2_loc (loc, code, TREE_TYPE (x), op0, op1);
3218 : : else
3219 : 62462761 : x = fold (x);
3220 : :
3221 : : /* This is only needed for -Wnonnull-compare and only if
3222 : : TREE_NO_WARNING (org_x), but to avoid that option affecting code
3223 : : generation, we do it always. */
3224 : 200470084 : if (COMPARISON_CLASS_P (org_x))
3225 : : {
3226 : 30925579 : if (x == error_mark_node || TREE_CODE (x) == INTEGER_CST)
3227 : : ;
3228 : 29813983 : else if (COMPARISON_CLASS_P (x))
3229 : : {
3230 : 28949024 : if (warn_nonnull_compare
3231 : 28949024 : && warning_suppressed_p (org_x, OPT_Wnonnull_compare))
3232 : 116719 : suppress_warning (x, OPT_Wnonnull_compare);
3233 : : }
3234 : : /* Otherwise give up on optimizing these, let GIMPLE folders
3235 : : optimize those later on. */
3236 : 864959 : else if (op0 != TREE_OPERAND (org_x, 0)
3237 : 864959 : || op1 != TREE_OPERAND (org_x, 1))
3238 : : {
3239 : 864459 : x = build2_loc (loc, code, TREE_TYPE (org_x), op0, op1);
3240 : 864459 : if (warn_nonnull_compare
3241 : 864459 : && warning_suppressed_p (org_x, OPT_Wnonnull_compare))
3242 : 16 : suppress_warning (x, OPT_Wnonnull_compare);
3243 : : }
3244 : : else
3245 : 500 : x = org_x;
3246 : : }
3247 : :
3248 : : break;
3249 : :
3250 : 4413010 : case VEC_COND_EXPR:
3251 : 4413010 : case COND_EXPR:
3252 : 4413010 : loc = EXPR_LOCATION (x);
3253 : 4413010 : op0 = cp_fold_rvalue (TREE_OPERAND (x, 0), flags);
3254 : 4413010 : op1 = cp_fold (TREE_OPERAND (x, 1), flags);
3255 : 4413010 : op2 = cp_fold (TREE_OPERAND (x, 2), flags);
3256 : :
3257 : 4413010 : if (TREE_CODE (TREE_TYPE (x)) == BOOLEAN_TYPE)
3258 : : {
3259 : 20057 : warning_sentinel s (warn_int_in_bool_context);
3260 : 20057 : if (!VOID_TYPE_P (TREE_TYPE (op1)))
3261 : 20057 : op1 = cp_truthvalue_conversion (op1, tf_warning_or_error);
3262 : 20057 : if (!VOID_TYPE_P (TREE_TYPE (op2)))
3263 : 20036 : op2 = cp_truthvalue_conversion (op2, tf_warning_or_error);
3264 : 20057 : }
3265 : 4392953 : else if (VOID_TYPE_P (TREE_TYPE (x)))
3266 : : {
3267 : 1256362 : if (TREE_CODE (op0) == INTEGER_CST)
3268 : : {
3269 : : /* If the condition is constant, fold can fold away
3270 : : the COND_EXPR. If some statement-level uses of COND_EXPR
3271 : : have one of the branches NULL, avoid folding crash. */
3272 : 181659 : if (!op1)
3273 : 0 : op1 = build_empty_stmt (loc);
3274 : 181659 : if (!op2)
3275 : 0 : op2 = build_empty_stmt (loc);
3276 : : }
3277 : : else
3278 : : {
3279 : : /* Otherwise, don't bother folding a void condition, since
3280 : : it can't produce a constant value. */
3281 : 1074703 : if (op0 != TREE_OPERAND (x, 0)
3282 : 1068103 : || op1 != TREE_OPERAND (x, 1)
3283 : 2026961 : || op2 != TREE_OPERAND (x, 2))
3284 : 123067 : x = build3_loc (loc, code, TREE_TYPE (x), op0, op1, op2);
3285 : : break;
3286 : : }
3287 : : }
3288 : :
3289 : 3338307 : if (op0 == error_mark_node
3290 : 3338307 : || op1 == error_mark_node
3291 : 3338299 : || op2 == error_mark_node)
3292 : 59 : x = error_mark_node;
3293 : 3338248 : else if (op0 != TREE_OPERAND (x, 0)
3294 : 1307585 : || op1 != TREE_OPERAND (x, 1)
3295 : 4360741 : || op2 != TREE_OPERAND (x, 2))
3296 : 2381305 : x = fold_build3_loc (loc, code, TREE_TYPE (x), op0, op1, op2);
3297 : : else
3298 : 956943 : x = fold (x);
3299 : :
3300 : : /* A COND_EXPR might have incompatible types in branches if one or both
3301 : : arms are bitfields. If folding exposed such a branch, fix it up. */
3302 : 3338307 : if (TREE_CODE (x) != code
3303 : 726590 : && x != error_mark_node
3304 : 4064838 : && !useless_type_conversion_p (TREE_TYPE (org_x), TREE_TYPE (x)))
3305 : 18065 : x = fold_convert (TREE_TYPE (org_x), x);
3306 : :
3307 : : break;
3308 : :
3309 : 106489885 : case CALL_EXPR:
3310 : 106489885 : {
3311 : 106489885 : tree callee = get_callee_fndecl (x);
3312 : :
3313 : : /* "Inline" calls to std::move/forward and other cast-like functions
3314 : : by simply folding them into a corresponding cast to their return
3315 : : type. This is cheaper than relying on the middle end to do so, and
3316 : : also means we avoid generating useless debug info for them at all.
3317 : :
3318 : : At this point the argument has already been converted into a
3319 : : reference, so it suffices to use a NOP_EXPR to express the
3320 : : cast. */
3321 : 106489885 : if ((OPTION_SET_P (flag_fold_simple_inlines)
3322 : 106489885 : ? flag_fold_simple_inlines
3323 : 106489799 : : !flag_no_inline)
3324 : 103326652 : && call_expr_nargs (x) == 1
3325 : 54901823 : && decl_in_std_namespace_p (callee)
3326 : 36141158 : && DECL_NAME (callee) != NULL_TREE
3327 : 142631043 : && (id_equal (DECL_NAME (callee), "move")
3328 : 35092728 : || id_equal (DECL_NAME (callee), "forward")
3329 : 34041468 : || id_equal (DECL_NAME (callee), "forward_like")
3330 : 34041466 : || id_equal (DECL_NAME (callee), "addressof")
3331 : : /* This addressof equivalent is used heavily in libstdc++. */
3332 : 33850419 : || id_equal (DECL_NAME (callee), "__addressof")
3333 : 33422072 : || id_equal (DECL_NAME (callee), "as_const")))
3334 : : {
3335 : 2719352 : r = CALL_EXPR_ARG (x, 0);
3336 : : /* Check that the return and argument types are sane before
3337 : : folding. */
3338 : 4819310 : if (INDIRECT_TYPE_P (TREE_TYPE (x))
3339 : 4819310 : && INDIRECT_TYPE_P (TREE_TYPE (r)))
3340 : : {
3341 : 2719352 : if (!same_type_p (TREE_TYPE (x), TREE_TYPE (r)))
3342 : 2458475 : r = build_nop (TREE_TYPE (x), r);
3343 : 2719352 : x = cp_fold (r, flags);
3344 : 2719352 : break;
3345 : : }
3346 : : }
3347 : :
3348 : 103770533 : int sv = optimize, nw = sv;
3349 : :
3350 : : /* Some built-in function calls will be evaluated at compile-time in
3351 : : fold (). Set optimize to 1 when folding __builtin_constant_p inside
3352 : : a constexpr function so that fold_builtin_1 doesn't fold it to 0. */
3353 : 102441191 : if (callee && fndecl_built_in_p (callee) && !optimize
3354 : 632927 : && DECL_IS_BUILTIN_CONSTANT_P (callee)
3355 : 9675 : && current_function_decl
3356 : 103780186 : && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
3357 : : nw = 1;
3358 : :
3359 : 103770533 : if (callee && fndecl_built_in_p (callee, BUILT_IN_FRONTEND))
3360 : : {
3361 : 34140 : iloc_sentinel ils (EXPR_LOCATION (x));
3362 : 34140 : switch (DECL_FE_FUNCTION_CODE (callee))
3363 : : {
3364 : 33268 : case CP_BUILT_IN_IS_CONSTANT_EVALUATED:
3365 : : /* Defer folding __builtin_is_constant_evaluated unless
3366 : : we know this isn't a manifestly constant-evaluated
3367 : : context. */
3368 : 33268 : if (flags & ff_mce_false)
3369 : 17012 : x = boolean_false_node;
3370 : : break;
3371 : 18 : case CP_BUILT_IN_SOURCE_LOCATION:
3372 : 18 : x = fold_builtin_source_location (x);
3373 : 18 : break;
3374 : 444 : case CP_BUILT_IN_IS_CORRESPONDING_MEMBER:
3375 : 888 : x = fold_builtin_is_corresponding_member
3376 : 444 : (EXPR_LOCATION (x), call_expr_nargs (x),
3377 : : &CALL_EXPR_ARG (x, 0));
3378 : 444 : break;
3379 : 410 : case CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS:
3380 : 820 : x = fold_builtin_is_pointer_inverconvertible_with_class
3381 : 410 : (EXPR_LOCATION (x), call_expr_nargs (x),
3382 : : &CALL_EXPR_ARG (x, 0));
3383 : 410 : break;
3384 : : default:
3385 : : break;
3386 : : }
3387 : 34140 : break;
3388 : 34140 : }
3389 : :
3390 : 103736393 : if (callee
3391 : 103736393 : && fndecl_built_in_p (callee, CP_BUILT_IN_SOURCE_LOCATION,
3392 : : BUILT_IN_FRONTEND))
3393 : : {
3394 : 0 : x = fold_builtin_source_location (x);
3395 : 0 : break;
3396 : : }
3397 : :
3398 : 103736393 : bool changed = false;
3399 : 103736393 : int m = call_expr_nargs (x);
3400 : 256248388 : for (int i = 0; i < m; i++)
3401 : : {
3402 : 152511995 : r = cp_fold (CALL_EXPR_ARG (x, i), flags);
3403 : 152511995 : if (r != CALL_EXPR_ARG (x, i))
3404 : : {
3405 : 89519186 : if (r == error_mark_node)
3406 : : {
3407 : 0 : x = error_mark_node;
3408 : 0 : break;
3409 : : }
3410 : 89519186 : if (!changed)
3411 : 56171306 : x = copy_node (x);
3412 : 89519186 : CALL_EXPR_ARG (x, i) = r;
3413 : 89519186 : changed = true;
3414 : : }
3415 : : }
3416 : 103736393 : if (x == error_mark_node)
3417 : : break;
3418 : :
3419 : 103736393 : optimize = nw;
3420 : 103736393 : r = fold (x);
3421 : 103736393 : optimize = sv;
3422 : :
3423 : 103736393 : if (TREE_CODE (r) != CALL_EXPR)
3424 : : {
3425 : 2400610 : x = cp_fold (r, flags);
3426 : 2400610 : break;
3427 : : }
3428 : :
3429 : 101335783 : optimize = nw;
3430 : :
3431 : : /* Invoke maybe_constant_value for functions declared
3432 : : constexpr and not called with AGGR_INIT_EXPRs.
3433 : : TODO:
3434 : : Do constexpr expansion of expressions where the call itself is not
3435 : : constant, but the call followed by an INDIRECT_REF is. */
3436 : 100006441 : if (callee && DECL_DECLARED_CONSTEXPR_P (callee)
3437 : 133321472 : && !flag_no_inline)
3438 : : {
3439 : 30973845 : mce_value manifestly_const_eval = mce_unknown;
3440 : 30973845 : if (flags & ff_mce_false)
3441 : : /* Allow folding __builtin_is_constant_evaluated to false during
3442 : : constexpr evaluation of this call. */
3443 : 22874845 : manifestly_const_eval = mce_false;
3444 : 30973845 : r = maybe_constant_value (x, /*decl=*/NULL_TREE,
3445 : : manifestly_const_eval);
3446 : : }
3447 : 101335783 : optimize = sv;
3448 : :
3449 : 101335783 : if (TREE_CODE (r) != CALL_EXPR)
3450 : : {
3451 : 6840188 : if (DECL_CONSTRUCTOR_P (callee))
3452 : 69942 : r = cp_build_init_expr_for_ctor (x, r);
3453 : 3420094 : x = r;
3454 : 3420094 : break;
3455 : : }
3456 : :
3457 : : break;
3458 : : }
3459 : :
3460 : 18284802 : case CONSTRUCTOR:
3461 : 18284802 : {
3462 : 18284802 : unsigned i;
3463 : 18284802 : constructor_elt *p;
3464 : 18284802 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (x);
3465 : 18284802 : vec<constructor_elt, va_gc> *nelts = NULL;
3466 : 41421104 : FOR_EACH_VEC_SAFE_ELT (elts, i, p)
3467 : : {
3468 : 23136302 : tree op = cp_fold (p->value, flags);
3469 : 23136302 : if (op == error_mark_node)
3470 : : {
3471 : 0 : x = error_mark_node;
3472 : 0 : vec_free (nelts);
3473 : : break;
3474 : : }
3475 : 23136302 : else if (op != p->value)
3476 : : {
3477 : 949914 : if (nelts == NULL)
3478 : 701138 : nelts = elts->copy ();
3479 : 949914 : (*nelts)[i].value = op;
3480 : : }
3481 : : }
3482 : 18284802 : if (nelts)
3483 : : {
3484 : 701138 : x = build_constructor (TREE_TYPE (x), nelts);
3485 : 701138 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY (x)
3486 : 701138 : = CONSTRUCTOR_PLACEHOLDER_BOUNDARY (org_x);
3487 : 701138 : CONSTRUCTOR_MUTABLE_POISON (x)
3488 : 1402276 : = CONSTRUCTOR_MUTABLE_POISON (org_x);
3489 : : }
3490 : 18284802 : if (VECTOR_TYPE_P (TREE_TYPE (x)))
3491 : 42611 : x = fold (x);
3492 : : break;
3493 : : }
3494 : 82800 : case TREE_VEC:
3495 : 82800 : {
3496 : 82800 : bool changed = false;
3497 : 82800 : int n = TREE_VEC_LENGTH (x);
3498 : :
3499 : 206518 : for (int i = 0; i < n; i++)
3500 : : {
3501 : 123718 : tree op = cp_fold (TREE_VEC_ELT (x, i), flags);
3502 : 123718 : if (op != TREE_VEC_ELT (x, i))
3503 : : {
3504 : 809 : if (!changed)
3505 : 774 : x = copy_node (x);
3506 : 809 : TREE_VEC_ELT (x, i) = op;
3507 : 809 : changed = true;
3508 : : }
3509 : : }
3510 : : }
3511 : :
3512 : : break;
3513 : :
3514 : 1656041 : case ARRAY_REF:
3515 : 1656041 : case ARRAY_RANGE_REF:
3516 : :
3517 : 1656041 : loc = EXPR_LOCATION (x);
3518 : 1656041 : op0 = cp_fold (TREE_OPERAND (x, 0), flags);
3519 : 1656041 : op1 = cp_fold (TREE_OPERAND (x, 1), flags);
3520 : 1656041 : op2 = cp_fold (TREE_OPERAND (x, 2), flags);
3521 : 1656041 : op3 = cp_fold (TREE_OPERAND (x, 3), flags);
3522 : :
3523 : 1656041 : if (op0 == error_mark_node
3524 : 1656041 : || op1 == error_mark_node
3525 : 1656041 : || op2 == error_mark_node
3526 : 1656041 : || op3 == error_mark_node)
3527 : 0 : x = error_mark_node;
3528 : 1656041 : else if (op0 != TREE_OPERAND (x, 0)
3529 : 725948 : || op1 != TREE_OPERAND (x, 1)
3530 : 371678 : || op2 != TREE_OPERAND (x, 2)
3531 : 2027719 : || op3 != TREE_OPERAND (x, 3))
3532 : : {
3533 : 1284363 : x = build4_loc (loc, code, TREE_TYPE (x), op0, op1, op2, op3);
3534 : 1284363 : TREE_READONLY (x) = TREE_READONLY (org_x);
3535 : 1284363 : TREE_SIDE_EFFECTS (x) = TREE_SIDE_EFFECTS (org_x);
3536 : 1284363 : TREE_THIS_VOLATILE (x) = TREE_THIS_VOLATILE (org_x);
3537 : : }
3538 : :
3539 : 1656041 : x = fold (x);
3540 : 1656041 : break;
3541 : :
3542 : 1071168 : case SAVE_EXPR:
3543 : : /* A SAVE_EXPR might contain e.g. (0 * i) + (0 * j), which, after
3544 : : folding, evaluates to an invariant. In that case no need to wrap
3545 : : this folded tree with a SAVE_EXPR. */
3546 : 1071168 : r = cp_fold (TREE_OPERAND (x, 0), flags);
3547 : 1071168 : if (tree_invariant_p (r))
3548 : 48 : x = r;
3549 : : break;
3550 : :
3551 : 8 : case REQUIRES_EXPR:
3552 : 8 : x = evaluate_requires_expr (x);
3553 : 8 : break;
3554 : :
3555 : : default:
3556 : : return org_x;
3557 : : }
3558 : :
3559 : 1155331509 : if (EXPR_P (x) && TREE_CODE (x) == code)
3560 : : {
3561 : 807824938 : TREE_THIS_VOLATILE (x) = TREE_THIS_VOLATILE (org_x);
3562 : 807824938 : copy_warning (x, org_x);
3563 : : }
3564 : :
3565 : 1155331509 : if (!c.evaluation_restricted_p ())
3566 : : {
3567 : 1153105451 : fold_cache->put (org_x, x);
3568 : : /* Prevent that we try to fold an already folded result again. */
3569 : 1153105451 : if (x != org_x)
3570 : 626606409 : fold_cache->put (x, x);
3571 : : }
3572 : :
3573 : : return x;
3574 : : }
3575 : :
3576 : : /* Look up "hot", "cold", "likely" or "unlikely" in attribute list LIST. */
3577 : :
3578 : : tree
3579 : 232592940 : lookup_hotness_attribute (tree list)
3580 : : {
3581 : 232609294 : for (; list; list = TREE_CHAIN (list))
3582 : : {
3583 : 542603 : tree name = get_attribute_name (list);
3584 : 542603 : if ((is_attribute_p ("hot", name)
3585 : 542603 : || is_attribute_p ("cold", name)
3586 : 542600 : || is_attribute_p ("likely", name)
3587 : 362356 : || is_attribute_p ("unlikely", name))
3588 : 1068861 : && is_attribute_namespace_p ("", list))
3589 : : break;
3590 : : }
3591 : 232592940 : return list;
3592 : : }
3593 : :
3594 : : /* Remove "hot", "cold", "likely" and "unlikely" attributes from LIST. */
3595 : :
3596 : : static tree
3597 : 526240 : remove_hotness_attribute (tree list)
3598 : : {
3599 : 1052498 : for (tree *p = &list; *p; )
3600 : : {
3601 : 526258 : tree l = *p;
3602 : 526258 : tree name = get_attribute_name (l);
3603 : 526258 : if ((is_attribute_p ("hot", name)
3604 : 526258 : || is_attribute_p ("cold", name)
3605 : 526255 : || is_attribute_p ("likely", name)
3606 : 346011 : || is_attribute_p ("unlikely", name))
3607 : 1052516 : && is_attribute_namespace_p ("", l))
3608 : : {
3609 : 526249 : *p = TREE_CHAIN (l);
3610 : 526249 : continue;
3611 : : }
3612 : 9 : p = &TREE_CHAIN (l);
3613 : : }
3614 : 526240 : return list;
3615 : : }
3616 : :
3617 : : /* If [[likely]] or [[unlikely]] appear on this statement, turn it into a
3618 : : PREDICT_EXPR. */
3619 : :
3620 : : tree
3621 : 232066718 : process_stmt_hotness_attribute (tree std_attrs, location_t attrs_loc)
3622 : : {
3623 : 232066718 : if (std_attrs == error_mark_node)
3624 : : return std_attrs;
3625 : 232066700 : if (tree attr = lookup_hotness_attribute (std_attrs))
3626 : : {
3627 : 526240 : tree name = get_attribute_name (attr);
3628 : 526240 : bool hot = (is_attribute_p ("hot", name)
3629 : 526240 : || is_attribute_p ("likely", name));
3630 : 526240 : tree pred = build_predict_expr (hot ? PRED_HOT_LABEL : PRED_COLD_LABEL,
3631 : : hot ? TAKEN : NOT_TAKEN);
3632 : 526240 : SET_EXPR_LOCATION (pred, attrs_loc);
3633 : 526240 : add_stmt (pred);
3634 : 526240 : if (tree other = lookup_hotness_attribute (TREE_CHAIN (attr)))
3635 : : {
3636 : 9 : auto_urlify_attributes sentinel;
3637 : 9 : warning (OPT_Wattributes, "ignoring attribute %qE after earlier %qE",
3638 : : get_attribute_name (other), name);
3639 : 9 : }
3640 : 526240 : std_attrs = remove_hotness_attribute (std_attrs);
3641 : : }
3642 : : return std_attrs;
3643 : : }
3644 : :
3645 : : /* Build IFN_ASSUME internal call for assume condition ARG. */
3646 : :
3647 : : tree
3648 : 10746 : build_assume_call (location_t loc, tree arg)
3649 : : {
3650 : 10746 : if (!processing_template_decl)
3651 : 10701 : arg = fold_build_cleanup_point_expr (TREE_TYPE (arg), arg);
3652 : 10746 : return build_call_expr_internal_loc (loc, IFN_ASSUME, void_type_node,
3653 : 10746 : 1, arg);
3654 : : }
3655 : :
3656 : : /* If [[assume (cond)]] appears on this statement, handle it. */
3657 : :
3658 : : tree
3659 : 192465918 : process_stmt_assume_attribute (tree std_attrs, tree statement,
3660 : : location_t attrs_loc)
3661 : : {
3662 : 192465918 : if (std_attrs == error_mark_node)
3663 : : return std_attrs;
3664 : 192465900 : tree attr = lookup_attribute ("gnu", "assume", std_attrs);
3665 : 192465900 : if (!attr)
3666 : : return std_attrs;
3667 : : /* The next token after the assume attribute is not ';'. */
3668 : 10669 : if (statement)
3669 : : {
3670 : 12 : warning_at (attrs_loc, OPT_Wattributes,
3671 : : "%<assume%> attribute not followed by %<;%>");
3672 : 12 : attr = NULL_TREE;
3673 : : }
3674 : 21362 : for (; attr; attr = lookup_attribute ("gnu", "assume", TREE_CHAIN (attr)))
3675 : : {
3676 : 10693 : tree args = TREE_VALUE (attr);
3677 : 10693 : if (args && PACK_EXPANSION_P (args))
3678 : : {
3679 : 6 : auto_diagnostic_group d;
3680 : 6 : error_at (attrs_loc, "pack expansion of %qE attribute",
3681 : : get_attribute_name (attr));
3682 : 6 : if (cxx_dialect >= cxx17)
3683 : 4 : inform (attrs_loc, "use fold expression in the attribute "
3684 : : "argument instead");
3685 : 6 : continue;
3686 : 6 : }
3687 : 10687 : int nargs = list_length (args);
3688 : 10687 : if (nargs != 1)
3689 : : {
3690 : 42 : auto_diagnostic_group d;
3691 : 42 : error_at (attrs_loc, "wrong number of arguments specified for "
3692 : : "%qE attribute", get_attribute_name (attr));
3693 : 42 : inform (attrs_loc, "expected %i, found %i", 1, nargs);
3694 : 42 : }
3695 : : else
3696 : : {
3697 : 10645 : tree arg = TREE_VALUE (args);
3698 : 10645 : if (!type_dependent_expression_p (arg))
3699 : 10600 : arg = contextual_conv_bool (arg, tf_warning_or_error);
3700 : 10645 : if (error_operand_p (arg))
3701 : 18 : continue;
3702 : 10627 : finish_expr_stmt (build_assume_call (attrs_loc, arg));
3703 : : }
3704 : : }
3705 : 10669 : return remove_attribute ("gnu", "assume", std_attrs);
3706 : : }
3707 : :
3708 : : /* Return the type std::source_location::__impl after performing
3709 : : verification on it. */
3710 : :
3711 : : tree
3712 : 2133 : get_source_location_impl_type ()
3713 : : {
3714 : 2133 : tree name = get_identifier ("source_location");
3715 : 2133 : tree decl = lookup_qualified_name (std_node, name);
3716 : 2133 : if (TREE_CODE (decl) != TYPE_DECL)
3717 : : {
3718 : 6 : auto_diagnostic_group d;
3719 : 6 : if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
3720 : 3 : qualified_name_lookup_error (std_node, name, decl, input_location);
3721 : : else
3722 : 3 : error ("%qD is not a type", decl);
3723 : 6 : return error_mark_node;
3724 : 6 : }
3725 : 2127 : name = get_identifier ("__impl");
3726 : 2127 : tree type = TREE_TYPE (decl);
3727 : 2127 : decl = lookup_qualified_name (type, name);
3728 : 2127 : if (TREE_CODE (decl) != TYPE_DECL)
3729 : : {
3730 : 9 : auto_diagnostic_group d;
3731 : 9 : if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
3732 : 6 : qualified_name_lookup_error (type, name, decl, input_location);
3733 : : else
3734 : 3 : error ("%qD is not a type", decl);
3735 : 9 : return error_mark_node;
3736 : 9 : }
3737 : 2118 : type = TREE_TYPE (decl);
3738 : 2118 : if (TREE_CODE (type) != RECORD_TYPE)
3739 : : {
3740 : 3 : error ("%qD is not a class type", decl);
3741 : 3 : return error_mark_node;
3742 : : }
3743 : :
3744 : 2115 : int cnt = 0;
3745 : 2115 : for (tree field = TYPE_FIELDS (type);
3746 : 10536 : (field = next_aggregate_field (field)) != NULL_TREE;
3747 : 8421 : field = DECL_CHAIN (field))
3748 : : {
3749 : 8430 : if (DECL_NAME (field) != NULL_TREE)
3750 : : {
3751 : 8430 : const char *n = IDENTIFIER_POINTER (DECL_NAME (field));
3752 : 8430 : if (strcmp (n, "_M_file_name") == 0
3753 : 6318 : || strcmp (n, "_M_function_name") == 0)
3754 : : {
3755 : 4221 : if (TREE_TYPE (field) != const_string_type_node)
3756 : : {
3757 : 3 : error ("%qD does not have %<const char *%> type", field);
3758 : 3 : return error_mark_node;
3759 : : }
3760 : 4218 : cnt++;
3761 : 4218 : continue;
3762 : : }
3763 : 4209 : else if (strcmp (n, "_M_line") == 0 || strcmp (n, "_M_column") == 0)
3764 : : {
3765 : 4206 : if (TREE_CODE (TREE_TYPE (field)) != INTEGER_TYPE)
3766 : : {
3767 : 3 : error ("%qD does not have integral type", field);
3768 : 3 : return error_mark_node;
3769 : : }
3770 : 4203 : cnt++;
3771 : 4203 : continue;
3772 : : }
3773 : : }
3774 : : cnt = 0;
3775 : : break;
3776 : : }
3777 : 2106 : if (cnt != 4)
3778 : : {
3779 : 9 : error ("%<std::source_location::__impl%> does not contain only "
3780 : : "non-static data members %<_M_file_name%>, "
3781 : : "%<_M_function_name%>, %<_M_line%> and %<_M_column%>");
3782 : 9 : return error_mark_node;
3783 : : }
3784 : 2100 : return build_qualified_type (type, TYPE_QUAL_CONST);
3785 : : }
3786 : :
3787 : : /* Type for source_location_table hash_set. */
3788 : : struct GTY((for_user)) source_location_table_entry {
3789 : : location_t loc;
3790 : : unsigned uid;
3791 : : tree var;
3792 : : };
3793 : :
3794 : : /* Traits class for function start hash maps below. */
3795 : :
3796 : : struct source_location_table_entry_hash
3797 : : : ggc_remove <source_location_table_entry>
3798 : : {
3799 : : typedef source_location_table_entry value_type;
3800 : : typedef source_location_table_entry compare_type;
3801 : :
3802 : : static hashval_t
3803 : 585 : hash (const source_location_table_entry &ref)
3804 : : {
3805 : 585 : inchash::hash hstate (0);
3806 : 585 : hstate.add_int (ref.loc);
3807 : 585 : hstate.add_int (ref.uid);
3808 : 585 : return hstate.end ();
3809 : : }
3810 : :
3811 : : static bool
3812 : 365 : equal (const source_location_table_entry &ref1,
3813 : : const source_location_table_entry &ref2)
3814 : : {
3815 : 365 : return ref1.loc == ref2.loc && ref1.uid == ref2.uid;
3816 : : }
3817 : :
3818 : : static void
3819 : : mark_deleted (source_location_table_entry &ref)
3820 : : {
3821 : : ref.loc = UNKNOWN_LOCATION;
3822 : : ref.uid = -1U;
3823 : : ref.var = NULL_TREE;
3824 : : }
3825 : :
3826 : : static const bool empty_zero_p = true;
3827 : :
3828 : : static void
3829 : 0 : mark_empty (source_location_table_entry &ref)
3830 : : {
3831 : 0 : ref.loc = UNKNOWN_LOCATION;
3832 : 0 : ref.uid = 0;
3833 : 0 : ref.var = NULL_TREE;
3834 : : }
3835 : :
3836 : : static bool
3837 : 389 : is_deleted (const source_location_table_entry &ref)
3838 : : {
3839 : 389 : return (ref.loc == UNKNOWN_LOCATION
3840 : 1 : && ref.uid == -1U
3841 : 389 : && ref.var == NULL_TREE);
3842 : : }
3843 : :
3844 : : static bool
3845 : 6320 : is_empty (const source_location_table_entry &ref)
3846 : : {
3847 : 6320 : return (ref.loc == UNKNOWN_LOCATION
3848 : 5348 : && ref.uid == 0
3849 : 11665 : && ref.var == NULL_TREE);
3850 : : }
3851 : :
3852 : : static void
3853 : 3 : pch_nx (source_location_table_entry &p)
3854 : : {
3855 : 3 : extern void gt_pch_nx (source_location_table_entry &);
3856 : 3 : gt_pch_nx (p);
3857 : 3 : }
3858 : :
3859 : : static void
3860 : 3 : pch_nx (source_location_table_entry &p, gt_pointer_operator op, void *cookie)
3861 : : {
3862 : 3 : extern void gt_pch_nx (source_location_table_entry *, gt_pointer_operator,
3863 : : void *);
3864 : 3 : gt_pch_nx (&p, op, cookie);
3865 : 3 : }
3866 : : };
3867 : :
3868 : : static GTY(()) hash_table <source_location_table_entry_hash>
3869 : : *source_location_table;
3870 : : static GTY(()) unsigned int source_location_id;
3871 : :
3872 : : /* Fold the __builtin_source_location () call T. */
3873 : :
3874 : : tree
3875 : 421 : fold_builtin_source_location (const_tree t)
3876 : : {
3877 : 421 : gcc_assert (TREE_CODE (t) == CALL_EXPR);
3878 : : /* TREE_TYPE (t) is const std::source_location::__impl* */
3879 : 421 : tree source_location_impl = TREE_TYPE (TREE_TYPE (t));
3880 : 421 : if (source_location_impl == error_mark_node)
3881 : 0 : return build_zero_cst (const_ptr_type_node);
3882 : 421 : gcc_assert (CLASS_TYPE_P (source_location_impl)
3883 : : && id_equal (TYPE_IDENTIFIER (source_location_impl), "__impl"));
3884 : :
3885 : 421 : location_t loc = EXPR_LOCATION (t);
3886 : 421 : if (source_location_table == NULL)
3887 : 40 : source_location_table
3888 : 40 : = hash_table <source_location_table_entry_hash>::create_ggc (64);
3889 : 421 : const line_map_ordinary *map;
3890 : 421 : source_location_table_entry entry;
3891 : 421 : entry.loc
3892 : 421 : = linemap_resolve_location (line_table, loc, LRK_MACRO_EXPANSION_POINT,
3893 : : &map);
3894 : 421 : entry.uid = current_function_decl ? DECL_UID (current_function_decl) : -1;
3895 : 421 : entry.var = error_mark_node;
3896 : 421 : source_location_table_entry *entryp
3897 : 421 : = source_location_table->find_slot (entry, INSERT);
3898 : 421 : tree var;
3899 : 421 : if (entryp->var)
3900 : : var = entryp->var;
3901 : : else
3902 : : {
3903 : 212 : char tmp_name[32];
3904 : 212 : ASM_GENERATE_INTERNAL_LABEL (tmp_name, "Lsrc_loc", source_location_id++);
3905 : 212 : var = build_decl (loc, VAR_DECL, get_identifier (tmp_name),
3906 : : source_location_impl);
3907 : 212 : TREE_STATIC (var) = 1;
3908 : 212 : TREE_PUBLIC (var) = 0;
3909 : 212 : DECL_ARTIFICIAL (var) = 1;
3910 : 212 : DECL_IGNORED_P (var) = 1;
3911 : 212 : DECL_EXTERNAL (var) = 0;
3912 : 212 : DECL_DECLARED_CONSTEXPR_P (var) = 1;
3913 : 212 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var) = 1;
3914 : 212 : layout_decl (var, 0);
3915 : :
3916 : 212 : vec<constructor_elt, va_gc> *v = NULL;
3917 : 212 : vec_alloc (v, 4);
3918 : 212 : for (tree field = TYPE_FIELDS (source_location_impl);
3919 : 1060 : (field = next_aggregate_field (field)) != NULL_TREE;
3920 : 848 : field = DECL_CHAIN (field))
3921 : : {
3922 : 848 : const char *n = IDENTIFIER_POINTER (DECL_NAME (field));
3923 : 848 : tree val = NULL_TREE;
3924 : 848 : if (strcmp (n, "_M_file_name") == 0)
3925 : : {
3926 : 212 : if (const char *fname = LOCATION_FILE (loc))
3927 : : {
3928 : 211 : fname = remap_macro_filename (fname);
3929 : 211 : val = build_string_literal (fname);
3930 : : }
3931 : : else
3932 : 1 : val = build_string_literal ("");
3933 : : }
3934 : 636 : else if (strcmp (n, "_M_function_name") == 0)
3935 : : {
3936 : 212 : const char *name = "";
3937 : :
3938 : 212 : if (current_function_decl)
3939 : : {
3940 : : /* If this is a coroutine, we should get the name of the user
3941 : : function rather than the actor we generate. */
3942 : 167 : if (tree ramp = DECL_RAMP_FN (current_function_decl))
3943 : 3 : name = cxx_printable_name (ramp, 2);
3944 : : else
3945 : 164 : name = cxx_printable_name (current_function_decl, 2);
3946 : : }
3947 : :
3948 : 212 : val = build_string_literal (name);
3949 : : }
3950 : 424 : else if (strcmp (n, "_M_line") == 0)
3951 : 212 : val = build_int_cst (TREE_TYPE (field), LOCATION_LINE (loc));
3952 : 212 : else if (strcmp (n, "_M_column") == 0)
3953 : 212 : val = build_int_cst (TREE_TYPE (field), LOCATION_COLUMN (loc));
3954 : : else
3955 : 0 : gcc_unreachable ();
3956 : 848 : CONSTRUCTOR_APPEND_ELT (v, field, val);
3957 : : }
3958 : :
3959 : 212 : tree ctor = build_constructor (source_location_impl, v);
3960 : 212 : TREE_CONSTANT (ctor) = 1;
3961 : 212 : TREE_STATIC (ctor) = 1;
3962 : 212 : DECL_INITIAL (var) = ctor;
3963 : 212 : varpool_node::finalize_decl (var);
3964 : 212 : *entryp = entry;
3965 : 212 : entryp->var = var;
3966 : : }
3967 : :
3968 : 421 : return build_fold_addr_expr_with_type_loc (loc, var, TREE_TYPE (t));
3969 : : }
3970 : :
3971 : : #include "gt-cp-cp-gimplify.h"
|