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 : 9014597 : remember_escalating_expr (tree t)
56 : : {
57 : 9014597 : 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 : 9014597 : if (!deferred_escalating_exprs)
62 : 6187 : deferred_escalating_exprs = hash_set<tree>::create_ggc (37);
63 : 9014597 : 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 : 93337260 : struct cp_fold_data
81 : : {
82 : : hash_set<tree> pset;
83 : : fold_flags_t flags;
84 : 93337260 : 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 : 16548 : genericize_try_block (tree *stmt_p)
99 : : {
100 : 16548 : tree body = TRY_STMTS (*stmt_p);
101 : 16548 : tree cleanup = TRY_HANDLERS (*stmt_p);
102 : :
103 : 16548 : *stmt_p = build2 (TRY_CATCH_EXPR, void_type_node, body, cleanup);
104 : 16548 : }
105 : :
106 : : /* Genericize a HANDLER by converting to a CATCH_EXPR. */
107 : :
108 : : static void
109 : 19521 : genericize_catch_block (tree *stmt_p)
110 : : {
111 : 19521 : tree type = HANDLER_TYPE (*stmt_p);
112 : 19521 : tree body = HANDLER_BODY (*stmt_p);
113 : :
114 : : /* FIXME should the caught type go in TREE_TYPE? */
115 : 19521 : *stmt_p = build2 (CATCH_EXPR, void_type_node, type, body);
116 : 19521 : }
117 : :
118 : : /* A terser interface for building a representation of an exception
119 : : specification. */
120 : :
121 : : static tree
122 : 4569 : build_gimple_eh_filter_tree (tree body, tree allowed, tree failure)
123 : : {
124 : 4569 : tree t;
125 : :
126 : : /* FIXME should the allowed types go in TREE_TYPE? */
127 : 4569 : t = build2 (EH_FILTER_EXPR, void_type_node, allowed, NULL_TREE);
128 : 4569 : append_to_statement_list (failure, &EH_FILTER_FAILURE (t));
129 : :
130 : 4569 : t = build2 (TRY_CATCH_EXPR, void_type_node, NULL_TREE, t);
131 : 4569 : append_to_statement_list (body, &TREE_OPERAND (t, 0));
132 : :
133 : 4569 : 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 : 4569 : genericize_eh_spec_block (tree *stmt_p)
141 : : {
142 : 4569 : tree body = EH_SPEC_STMTS (*stmt_p);
143 : 4569 : tree allowed = EH_SPEC_RAISES (*stmt_p);
144 : 4569 : tree failure = build_call_n (call_unexpected_fn, 1, build_exc_ptr ());
145 : :
146 : 4569 : *stmt_p = build_gimple_eh_filter_tree (body, allowed, failure);
147 : 4569 : suppress_warning (*stmt_p);
148 : 4569 : suppress_warning (TREE_OPERAND (*stmt_p, 1));
149 : 4569 : }
150 : :
151 : : /* Return the first non-compound statement in STMT. */
152 : :
153 : : tree
154 : 12495362 : first_stmt (tree stmt)
155 : : {
156 : 19643458 : switch (TREE_CODE (stmt))
157 : : {
158 : 4682024 : case STATEMENT_LIST:
159 : 4682024 : if (tree_statement_list_node *p = STATEMENT_LIST_HEAD (stmt))
160 : 3367661 : return first_stmt (p->stmt);
161 : 1314363 : return void_node;
162 : :
163 : 3780435 : case BIND_EXPR:
164 : 3780435 : 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 : 17410209 : genericize_if_stmt (tree *stmt_p)
175 : : {
176 : 17410209 : tree stmt, cond, then_, else_;
177 : 17410209 : location_t locus = EXPR_LOCATION (*stmt_p);
178 : :
179 : 17410209 : stmt = *stmt_p;
180 : 17410209 : cond = IF_COND (stmt);
181 : 17410209 : then_ = THEN_CLAUSE (stmt);
182 : 17410209 : else_ = ELSE_CLAUSE (stmt);
183 : :
184 : 17410209 : if (then_ && else_)
185 : : {
186 : 6247681 : tree ft = first_stmt (then_);
187 : 6247681 : tree fe = first_stmt (else_);
188 : 6247681 : br_predictor pr;
189 : 6247681 : if (TREE_CODE (ft) == PREDICT_EXPR
190 : 45999 : && TREE_CODE (fe) == PREDICT_EXPR
191 : 48 : && (pr = PREDICT_EXPR_PREDICTOR (ft)) == PREDICT_EXPR_PREDICTOR (fe)
192 : 6247720 : && (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 : 17410209 : if (!then_)
203 : 1908 : then_ = build_empty_stmt (locus);
204 : 17410209 : if (!else_)
205 : 11160662 : 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 : 17410209 : if (IF_STMT_CONSTEVAL_P (stmt))
212 : : {
213 : 17596 : if (block_may_fallthru (then_))
214 : 4055 : stmt = build3 (COND_EXPR, void_type_node, boolean_false_node,
215 : : void_node, else_);
216 : : else
217 : : stmt = else_;
218 : : }
219 : 17392613 : else if (IF_STMT_CONSTEXPR_P (stmt))
220 : 2983616 : 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 : 14457171 : else if (integer_nonzerop (cond) && !TREE_SIDE_EFFECTS (else_))
224 : : stmt = then_;
225 : 14392634 : else if (integer_zerop (cond) && !TREE_SIDE_EFFECTS (then_))
226 : : stmt = else_;
227 : : else
228 : 14358001 : stmt = build3 (COND_EXPR, void_type_node, cond, then_, else_);
229 : 17410209 : protected_set_expr_location_if_unset (stmt, locus);
230 : 17410209 : *stmt_p = stmt;
231 : 17410209 : }
232 : :
233 : : /* Hook into the middle of gimplifying an OMP_FOR node. */
234 : :
235 : : static enum gimplify_status
236 : 45350 : cp_gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
237 : : {
238 : 45350 : tree for_stmt = *expr_p;
239 : 45350 : gimple_seq seq = NULL;
240 : :
241 : : /* Protect ourselves from recursion. */
242 : 45350 : if (OMP_FOR_GIMPLIFYING_P (for_stmt))
243 : : return GS_UNHANDLED;
244 : 21069 : OMP_FOR_GIMPLIFYING_P (for_stmt) = 1;
245 : :
246 : 21069 : gimplify_and_add (for_stmt, &seq);
247 : 21069 : gimple_seq_add_seq (pre_p, seq);
248 : :
249 : 21069 : OMP_FOR_GIMPLIFYING_P (for_stmt) = 0;
250 : :
251 : 21069 : return GS_ALL_DONE;
252 : : }
253 : :
254 : : /* Gimplify an EXPR_STMT node. */
255 : :
256 : : static void
257 : 4178277 : gimplify_expr_stmt (tree *stmt_p)
258 : : {
259 : 4178277 : tree stmt = EXPR_STMT_EXPR (*stmt_p);
260 : :
261 : 4178277 : 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 : 4174448 : if (stmt && warn_unused_value)
271 : : {
272 : 301572 : if (!TREE_SIDE_EFFECTS (stmt))
273 : : {
274 : 0 : if (!IS_EMPTY_STMT (stmt)
275 : 7097 : && !VOID_TYPE_P (TREE_TYPE (stmt))
276 : 7097 : && !warning_suppressed_p (stmt, OPT_Wunused_value))
277 : 0 : warning (OPT_Wunused_value, "statement with no effect");
278 : : }
279 : : else
280 : 294475 : warn_if_unused_value (stmt, input_location);
281 : : }
282 : :
283 : 4178277 : if (stmt == NULL_TREE)
284 : 3829 : stmt = alloc_stmt_list ();
285 : :
286 : 4178277 : *stmt_p = stmt;
287 : 4178277 : }
288 : :
289 : : /* Gimplify initialization from an AGGR_INIT_EXPR. */
290 : :
291 : : static void
292 : 11550204 : cp_gimplify_init_expr (tree *expr_p)
293 : : {
294 : 11550204 : tree from = TREE_OPERAND (*expr_p, 1);
295 : 11550204 : tree to = TREE_OPERAND (*expr_p, 0);
296 : 11550204 : tree t;
297 : :
298 : 11550204 : if (TREE_CODE (from) == TARGET_EXPR)
299 : 212708 : 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 : 212708 : gcc_checking_assert (TARGET_EXPR_ELIDING_P (from)
304 : : || !TREE_ADDRESSABLE (TREE_TYPE (from)));
305 : 212708 : 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 : 107 : replace_decl (&init, TARGET_EXPR_SLOT (from), to);
310 : 107 : *expr_p = init;
311 : 107 : 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 : 11618182 : for (t = from; t; )
320 : : {
321 : 11618182 : 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 : 11618182 : if (TREE_CODE (sub) == AGGR_INIT_EXPR
329 : 11618182 : || TREE_CODE (sub) == VEC_INIT_EXPR)
330 : : {
331 : 113153 : if (TREE_CODE (sub) == AGGR_INIT_EXPR)
332 : 113153 : AGGR_INIT_EXPR_SLOT (sub) = to;
333 : : else
334 : 0 : VEC_INIT_EXPR_SLOT (sub) = to;
335 : 113153 : *expr_p = from;
336 : :
337 : : /* The initialization is now a side-effect, so the container can
338 : : become void. */
339 : 113153 : if (from != sub)
340 : 73 : TREE_TYPE (from) = void_type_node;
341 : : }
342 : :
343 : : /* Handle aggregate NSDMI. */
344 : 11618182 : replace_placeholders (sub, to);
345 : :
346 : 11618182 : if (t == sub)
347 : : break;
348 : : else
349 : 68085 : t = TREE_OPERAND (t, 1);
350 : : }
351 : :
352 : : }
353 : :
354 : : /* Gimplify a MUST_NOT_THROW_EXPR. */
355 : :
356 : : static enum gimplify_status
357 : 597111 : gimplify_must_not_throw_expr (tree *expr_p, gimple_seq *pre_p)
358 : : {
359 : 597111 : tree stmt = *expr_p;
360 : 597111 : tree temp = voidify_wrapper_expr (stmt, NULL);
361 : 597111 : tree body = TREE_OPERAND (stmt, 0);
362 : 597111 : gimple_seq try_ = NULL;
363 : 597111 : gimple_seq catch_ = NULL;
364 : 597111 : gimple *mnt;
365 : :
366 : 597111 : gimplify_and_add (body, &try_);
367 : 597111 : mnt = gimple_build_eh_must_not_throw (call_terminate_fn);
368 : 597111 : gimple_seq_add_stmt_without_update (&catch_, mnt);
369 : 597111 : mnt = gimple_build_try (try_, catch_, GIMPLE_TRY_CATCH);
370 : :
371 : 597111 : gimple_seq_add_stmt_without_update (pre_p, mnt);
372 : 597111 : if (temp)
373 : : {
374 : 33 : *expr_p = temp;
375 : 33 : return GS_OK;
376 : : }
377 : :
378 : 597078 : *expr_p = NULL;
379 : 597078 : 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 : 18642726 : simple_empty_class_p (tree type, tree op, tree_code code)
391 : : {
392 : 21652055 : if (TREE_CODE (op) == COMPOUND_EXPR)
393 : 146660 : return simple_empty_class_p (type, TREE_OPERAND (op, 1), code);
394 : 3339026 : if (SIMPLE_TARGET_EXPR_P (op)
395 : 24371623 : && TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
396 : : /* The TARGET_EXPR is itself a simple copy, look through it. */
397 : 2862669 : return simple_empty_class_p (type, TARGET_EXPR_INITIAL (op), code);
398 : :
399 : 18642726 : if (TREE_CODE (op) == PARM_DECL
400 : 18642726 : && 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 : 18642723 : return
411 : 18642723 : (TREE_CODE (op) == EMPTY_CLASS_EXPR
412 : 18642693 : || code == MODIFY_EXPR
413 : 15579956 : || is_gimple_lvalue (op)
414 : 12152685 : || INDIRECT_REF_P (op)
415 : 11782522 : || (TREE_CODE (op) == CONSTRUCTOR
416 : 2443837 : && CONSTRUCTOR_NELTS (op) == 0)
417 : 9539891 : || (TREE_CODE (op) == CALL_EXPR
418 : 2717197 : && !CALL_EXPR_RETURN_SLOT_OPT (op)))
419 : 11715428 : && !TREE_CLOBBER_P (op)
420 : 29604675 : && 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 : 2720489 : lvalue_has_side_effects (tree e)
429 : : {
430 : 2720489 : if (!TREE_SIDE_EFFECTS (e))
431 : : return false;
432 : 57619 : while (handled_component_p (e))
433 : : {
434 : 3148 : if (TREE_CODE (e) == ARRAY_REF
435 : 3148 : && TREE_SIDE_EFFECTS (TREE_OPERAND (e, 1)))
436 : : return true;
437 : 1649 : e = TREE_OPERAND (e, 0);
438 : : }
439 : 54471 : if (DECL_P (e))
440 : : /* Just naming a variable has no side-effects. */
441 : : return false;
442 : 38296 : else if (INDIRECT_REF_P (e))
443 : : /* Similarly, indirection has no side-effects. */
444 : 38194 : return TREE_SIDE_EFFECTS (TREE_OPERAND (e, 0));
445 : : else
446 : : /* For anything else, trust TREE_SIDE_EFFECTS. */
447 : 102 : return TREE_SIDE_EFFECTS (e);
448 : : }
449 : :
450 : : /* Return true if FN is an immediate-escalating function. */
451 : :
452 : : static bool
453 : 72096731 : immediate_escalating_function_p (tree fn)
454 : : {
455 : 72096731 : if (!fn || !flag_immediate_escalation)
456 : : return false;
457 : :
458 : 72096315 : gcc_checking_assert (TREE_CODE (fn) == FUNCTION_DECL);
459 : :
460 : 72096315 : 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 : 74677001 : if (LAMBDA_FUNCTION_P (fn))
467 : : return true;
468 : : /* -- a defaulted special member function that is not declared with the
469 : : consteval specifier */
470 : 70566182 : special_function_kind sfk = special_memfn_p (fn);
471 : 70566182 : 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 : 69858091 : 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 : 72095729 : unchecked_immediate_escalating_function_p (tree fn)
483 : : {
484 : 72095729 : return (immediate_escalating_function_p (fn)
485 : 72095729 : && !DECL_ESCALATION_CHECKED_P (fn));
486 : : }
487 : :
488 : : /* Promote FN to an immediate function, including its clones. */
489 : :
490 : : static void
491 : 325 : promote_function_to_consteval (tree fn)
492 : : {
493 : 325 : SET_DECL_IMMEDIATE_FUNCTION_P (fn);
494 : 325 : DECL_ESCALATION_CHECKED_P (fn) = true;
495 : 325 : tree clone;
496 : 361 : FOR_EACH_CLONE (clone, fn)
497 : : {
498 : 36 : SET_DECL_IMMEDIATE_FUNCTION_P (clone);
499 : 36 : DECL_ESCALATION_CHECKED_P (clone) = true;
500 : : }
501 : 325 : }
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 : 5636154 : cp_fold_immediate (tree *tp, mce_value manifestly_const_eval,
509 : : tree decl /*= current_function_decl*/)
510 : : {
511 : 5636154 : if (cxx_dialect <= cxx17)
512 : : return NULL_TREE;
513 : :
514 : 5250246 : temp_override<tree> cfd (current_function_decl, decl);
515 : :
516 : 5250246 : fold_flags_t flags = ff_none;
517 : 5250246 : if (manifestly_const_eval == mce_false)
518 : 2976626 : flags |= ff_mce_false;
519 : :
520 : 5250246 : cp_fold_data data (flags);
521 : 5250246 : int save_errorcount = errorcount;
522 : 5250246 : tree r = cp_walk_tree (tp, cp_fold_immediate_r, &data, NULL);
523 : 5250246 : if (errorcount > save_errorcount)
524 : 49 : return integer_one_node;
525 : : return r;
526 : 5250246 : }
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 : 581 : maybe_explain_promoted_consteval (location_t loc, tree fn)
534 : : {
535 : 581 : 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 : 99 : 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 : 27 : inform (loc, "%qD was promoted to an immediate function", fn);
545 : : }
546 : 581 : }
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 : 36383 : gimplify_to_rvalue (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
553 : : bool (*gimple_test_f) (tree))
554 : : {
555 : 36383 : enum gimplify_status t
556 : 36383 : = gimplify_expr (expr_p, pre_p, post_p, gimple_test_f, fb_rvalue);
557 : 36383 : if (t == GS_ERROR)
558 : : return GS_ERROR;
559 : 36380 : else if (is_gimple_variable (*expr_p) && TREE_CODE (*expr_p) != SSA_NAME)
560 : 2934 : *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 : 4193618 : cp_gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location,
572 : : bool ordered)
573 : : {
574 : 4193618 : enum gimplify_status t;
575 : 4193618 : if (ordered
576 : 415247 : && !is_gimple_reg_type (TREE_TYPE (*arg_p))
577 : 4195736 : && 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 : 1970 : protected_set_expr_location (*arg_p, call_location);
583 : 1970 : return gimplify_expr (arg_p, pre_p, NULL, is_gimple_lvalue, fb_either);
584 : : }
585 : : else
586 : : {
587 : 4191648 : t = gimplify_arg (arg_p, pre_p, call_location);
588 : 4191648 : if (t == GS_ERROR)
589 : : return GS_ERROR;
590 : 4191648 : else if (ordered
591 : 413277 : && is_gimple_reg_type (TREE_TYPE (*arg_p))
592 : 413129 : && is_gimple_variable (*arg_p)
593 : 220502 : && TREE_CODE (*arg_p) != SSA_NAME
594 : : /* No need to force references into register, references
595 : : can't be modified. */
596 : 134190 : && !TYPE_REF_P (TREE_TYPE (*arg_p))
597 : : /* And this can't be modified either. */
598 : 4288944 : && *arg_p != current_class_ptr)
599 : 10012 : *arg_p = get_initialized_tmp_var (*arg_p, pre_p);
600 : 4191648 : return t;
601 : : }
602 : :
603 : : }
604 : :
605 : : /* Do C++-specific gimplification. Args are as for gimplify_expr. */
606 : :
607 : : int
608 : 172554348 : cp_gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
609 : : {
610 : 172554348 : int saved_stmts_are_full_exprs_p = 0;
611 : 172554348 : location_t loc = cp_expr_loc_or_input_loc (*expr_p);
612 : 172554348 : enum tree_code code = TREE_CODE (*expr_p);
613 : 172554348 : enum gimplify_status ret;
614 : :
615 : 172554348 : if (STATEMENT_CODE_P (code))
616 : : {
617 : 4218915 : saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
618 : 8437830 : current_stmt_tree ()->stmts_are_full_exprs_p
619 : 4218915 : = STMT_IS_FULL_EXPR_P (*expr_p);
620 : : }
621 : :
622 : 172554348 : switch (code)
623 : : {
624 : 380152 : case AGGR_INIT_EXPR:
625 : 380152 : simplify_aggr_init_expr (expr_p);
626 : 380152 : ret = GS_OK;
627 : 380152 : 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 : 17609 : case THROW_EXPR:
643 : : /* FIXME communicate throw type to back end, probably by moving
644 : : THROW_EXPR into ../tree.def. */
645 : 17609 : *expr_p = TREE_OPERAND (*expr_p, 0);
646 : 17609 : ret = GS_OK;
647 : 17609 : break;
648 : :
649 : 597111 : case MUST_NOT_THROW_EXPR:
650 : 597111 : ret = gimplify_must_not_throw_expr (expr_p, pre_p);
651 : 597111 : 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 : 11550204 : case INIT_EXPR:
657 : 11550204 : cp_gimplify_init_expr (expr_p);
658 : 11550204 : if (TREE_CODE (*expr_p) != INIT_EXPR)
659 : : return GS_OK;
660 : : /* Fall through. */
661 : 15634369 : case MODIFY_EXPR:
662 : 11436944 : modify_expr_case:
663 : 15634369 : {
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 : 15634369 : tree op0 = TREE_OPERAND (*expr_p, 0);
667 : 15634369 : tree op1 = TREE_OPERAND (*expr_p, 1);
668 : :
669 : 15634369 : if (!error_operand_p (op0)
670 : 15634369 : && !error_operand_p (op1)
671 : 15634350 : && (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (op0))
672 : 15631705 : || TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (op1)))
673 : 15637023 : && !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 : 15634360 : else if (simple_empty_class_p (TREE_TYPE (op0), op1, code))
678 : : {
679 : 171959 : while (TREE_CODE (op1) == TARGET_EXPR)
680 : : /* We're disconnecting the initializer from its target,
681 : : don't create a temporary. */
682 : 11509 : 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 : 160450 : if (TREE_SIDE_EFFECTS (op1))
688 : : {
689 : 13337 : if (TREE_THIS_VOLATILE (op1)
690 : 0 : && (REFERENCE_CLASS_P (op1) || DECL_P (op1)))
691 : 0 : op1 = build_fold_addr_expr (op1);
692 : :
693 : 13337 : gimplify_and_add (op1, pre_p);
694 : : }
695 : 160450 : gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
696 : : is_gimple_lvalue, fb_lvalue);
697 : 160450 : *expr_p = TREE_OPERAND (*expr_p, 0);
698 : 160450 : if (code == RETURN_EXPR && REFERENCE_CLASS_P (*expr_p))
699 : : /* Avoid 'return *<retval>;' */
700 : 7 : *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 : 15473910 : else if (flag_strong_eval_order > 1
715 : 14158071 : && TREE_CODE (*expr_p) == MODIFY_EXPR
716 : 2720489 : && lvalue_has_side_effects (op0)
717 : 15513567 : && (TREE_CODE (op1) == CALL_EXPR
718 : 32573 : || (SCALAR_TYPE_P (TREE_TYPE (op1))
719 : 27117 : && !TREE_CONSTANT (op1))))
720 : 22463 : TREE_OPERAND (*expr_p, 1) = get_initialized_tmp_var (op1, pre_p);
721 : : }
722 : : ret = GS_OK;
723 : : break;
724 : :
725 : 85126 : case EMPTY_CLASS_EXPR:
726 : : /* We create an empty CONSTRUCTOR with RECORD_TYPE. */
727 : 85126 : *expr_p = build_constructor (TREE_TYPE (*expr_p), NULL);
728 : 85126 : ret = GS_OK;
729 : 85126 : break;
730 : :
731 : 0 : case BASELINK:
732 : 0 : *expr_p = BASELINK_FUNCTIONS (*expr_p);
733 : 0 : ret = GS_OK;
734 : 0 : break;
735 : :
736 : 16548 : case TRY_BLOCK:
737 : 16548 : genericize_try_block (expr_p);
738 : 16548 : ret = GS_OK;
739 : 16548 : break;
740 : :
741 : 19521 : case HANDLER:
742 : 19521 : genericize_catch_block (expr_p);
743 : 19521 : ret = GS_OK;
744 : 19521 : break;
745 : :
746 : 4569 : case EH_SPEC_BLOCK:
747 : 4569 : genericize_eh_spec_block (expr_p);
748 : 4569 : ret = GS_OK;
749 : 4569 : 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 : 45350 : case OMP_FOR:
763 : 45350 : case OMP_SIMD:
764 : 45350 : case OMP_DISTRIBUTE:
765 : 45350 : case OMP_LOOP:
766 : 45350 : case OMP_TASKLOOP:
767 : 45350 : case OMP_TILE:
768 : 45350 : case OMP_UNROLL:
769 : 45350 : ret = cp_gimplify_omp_for (expr_p, pre_p);
770 : 45350 : break;
771 : :
772 : 4178277 : case EXPR_STMT:
773 : 4178277 : gimplify_expr_stmt (expr_p);
774 : 4178277 : ret = GS_OK;
775 : 4178277 : 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 : 8313809 : case CALL_EXPR:
788 : 8313809 : ret = GS_OK;
789 : 8313809 : if (flag_strong_eval_order == 2
790 : 7848195 : && CALL_EXPR_FN (*expr_p)
791 : 7657536 : && !CALL_EXPR_OPERATOR_SYNTAX (*expr_p)
792 : 15218410 : && cp_get_callee_fndecl_nofold (*expr_p) == NULL_TREE)
793 : : {
794 : 36383 : tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
795 : 36383 : enum gimplify_status t
796 : 36383 : = gimplify_to_rvalue (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
797 : : is_gimple_call_addr);
798 : 36383 : 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 : 36380 : else if (TREE_TYPE (CALL_EXPR_FN (*expr_p)) != fnptrtype)
803 : 8575 : CALL_EXPR_FN (*expr_p)
804 : 17150 : = build1 (NOP_EXPR, fnptrtype, CALL_EXPR_FN (*expr_p));
805 : : }
806 : 8313809 : if (!CALL_EXPR_FN (*expr_p))
807 : : /* Internal function call. */;
808 : 8100628 : 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 : 53162 : gcc_assert (call_expr_nargs (*expr_p) == 2);
813 : 53162 : gcc_assert (!CALL_EXPR_ORDERED_ARGS (*expr_p));
814 : 53162 : enum gimplify_status t
815 : 53162 : = cp_gimplify_arg (&CALL_EXPR_ARG (*expr_p, 1), pre_p, loc,
816 : 53162 : TREE_SIDE_EFFECTS (CALL_EXPR_ARG (*expr_p, 0)));
817 : 53162 : if (t == GS_ERROR)
818 : : ret = GS_ERROR;
819 : : }
820 : 8047466 : 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 : 177539 : int nargs = call_expr_nargs (*expr_p) - 1;
825 : 177539 : int last_side_effects_arg = -1;
826 : 354913 : for (int i = nargs; i > 0; --i)
827 : 195846 : if (TREE_SIDE_EFFECTS (CALL_EXPR_ARG (*expr_p, i)))
828 : : {
829 : : last_side_effects_arg = i;
830 : : break;
831 : : }
832 : 383787 : for (int i = 0; i < nargs; ++i)
833 : : {
834 : 206248 : enum gimplify_status t
835 : 206248 : = cp_gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p, loc,
836 : : i < last_side_effects_arg);
837 : 206248 : if (t == GS_ERROR)
838 : 0 : ret = GS_ERROR;
839 : : }
840 : : }
841 : 7869927 : else if (flag_strong_eval_order
842 : 7869927 : && !CALL_EXPR_OPERATOR_SYNTAX (*expr_p))
843 : : {
844 : : /* If flag_strong_eval_order, evaluate the object argument first. */
845 : 7285399 : tree fntype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
846 : 7285399 : if (INDIRECT_TYPE_P (fntype))
847 : 7285396 : fntype = TREE_TYPE (fntype);
848 : 7285399 : if (TREE_CODE (fntype) == METHOD_TYPE)
849 : : {
850 : 3934208 : int nargs = call_expr_nargs (*expr_p);
851 : 3934208 : bool side_effects = false;
852 : 5518414 : for (int i = 1; i < nargs; ++i)
853 : 1963675 : if (TREE_SIDE_EFFECTS (CALL_EXPR_ARG (*expr_p, i)))
854 : : {
855 : : side_effects = true;
856 : : break;
857 : : }
858 : 3934208 : enum gimplify_status t
859 : 3934208 : = cp_gimplify_arg (&CALL_EXPR_ARG (*expr_p, 0), pre_p, loc,
860 : : side_effects);
861 : 3934208 : if (t == GS_ERROR)
862 : : ret = GS_ERROR;
863 : : }
864 : : }
865 : 8313809 : if (ret != GS_ERROR)
866 : : {
867 : 8313806 : tree decl = cp_get_callee_fndecl_nofold (*expr_p);
868 : 8313806 : if (!decl)
869 : : break;
870 : 8059411 : 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 : 0 : case CP_BUILT_IN_EH_PTR_ADJUST_REF:
893 : 0 : error_at (EXPR_LOCATION (*expr_p),
894 : : "%qs used outside of constant expressions",
895 : : "__builtin_eh_ptr_adjust_ref");
896 : 0 : *expr_p = void_node;
897 : 0 : break;
898 : : default:
899 : : break;
900 : : }
901 : 8059411 : else if (fndecl_built_in_p (decl, BUILT_IN_CLZG, BUILT_IN_CTZG))
902 : 27 : ret = (enum gimplify_status) c_gimplify_expr (expr_p, pre_p,
903 : : post_p);
904 : : else
905 : : /* All consteval functions should have been processed by now. */
906 : 8059384 : gcc_checking_assert (!immediate_invocation_p (decl));
907 : : }
908 : : break;
909 : :
910 : 625518 : case TARGET_EXPR:
911 : : /* A TARGET_EXPR that expresses direct-initialization should have been
912 : : elided by cp_gimplify_init_expr. */
913 : 625518 : gcc_checking_assert (!TARGET_EXPR_DIRECT_INIT_P (*expr_p));
914 : : /* Likewise, but allow extra temps of trivial type so that
915 : : gimplify_init_ctor_preeval can materialize subobjects of a CONSTRUCTOR
916 : : on the rhs of an assignment, as in constexpr-aggr1.C. */
917 : 625518 : gcc_checking_assert (!TARGET_EXPR_ELIDING_P (*expr_p)
918 : : || !TREE_ADDRESSABLE (TREE_TYPE (*expr_p)));
919 : : ret = GS_UNHANDLED;
920 : : break;
921 : :
922 : 3 : case PTRMEM_CST:
923 : 3 : *expr_p = cplus_expand_constant (*expr_p);
924 : 3 : if (TREE_CODE (*expr_p) == PTRMEM_CST)
925 : : ret = GS_ERROR;
926 : : else
927 : 28649953 : ret = GS_OK;
928 : : break;
929 : :
930 : 1203496 : case RETURN_EXPR:
931 : 1203496 : if (TREE_OPERAND (*expr_p, 0)
932 : 1203496 : && (TREE_CODE (TREE_OPERAND (*expr_p, 0)) == INIT_EXPR
933 : 12444 : || TREE_CODE (TREE_OPERAND (*expr_p, 0)) == MODIFY_EXPR))
934 : : {
935 : 1134688 : expr_p = &TREE_OPERAND (*expr_p, 0);
936 : : /* Avoid going through the INIT_EXPR case, which can
937 : : degrade INIT_EXPRs into AGGR_INIT_EXPRs. */
938 : 1134688 : goto modify_expr_case;
939 : : }
940 : : /* Fall through. */
941 : :
942 : 142523126 : default:
943 : 142523126 : ret = (enum gimplify_status) c_gimplify_expr (expr_p, pre_p, post_p);
944 : 142523126 : break;
945 : : }
946 : :
947 : : /* Restore saved state. */
948 : 172441088 : if (STATEMENT_CODE_P (code))
949 : 4218915 : current_stmt_tree ()->stmts_are_full_exprs_p
950 : 4218915 : = saved_stmts_are_full_exprs_p;
951 : :
952 : : return ret;
953 : : }
954 : :
955 : : static inline bool
956 : 1676682848 : is_invisiref_parm (const_tree t)
957 : : {
958 : 1573462805 : return ((TREE_CODE (t) == PARM_DECL || TREE_CODE (t) == RESULT_DECL)
959 : 1712986410 : && DECL_BY_REFERENCE (t));
960 : : }
961 : :
962 : : /* A stable comparison routine for use with splay trees and DECLs. */
963 : :
964 : : static int
965 : 62062 : splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
966 : : {
967 : 62062 : tree a = (tree) xa;
968 : 62062 : tree b = (tree) xb;
969 : :
970 : 62062 : return DECL_UID (a) - DECL_UID (b);
971 : : }
972 : :
973 : : /* OpenMP context during genericization. */
974 : :
975 : : struct cp_genericize_omp_taskreg
976 : : {
977 : : bool is_parallel;
978 : : bool default_shared;
979 : : struct cp_genericize_omp_taskreg *outer;
980 : : splay_tree variables;
981 : : };
982 : :
983 : : /* Return true if genericization should try to determine if
984 : : DECL is firstprivate or shared within task regions. */
985 : :
986 : : static bool
987 : 118973 : omp_var_to_track (tree decl)
988 : : {
989 : 118973 : tree type = TREE_TYPE (decl);
990 : 118973 : if (is_invisiref_parm (decl))
991 : 537 : type = TREE_TYPE (type);
992 : 118436 : else if (TYPE_REF_P (type))
993 : 5451 : type = TREE_TYPE (type);
994 : 143942 : while (TREE_CODE (type) == ARRAY_TYPE)
995 : 24969 : type = TREE_TYPE (type);
996 : 118973 : if (type == error_mark_node || !CLASS_TYPE_P (type))
997 : : return false;
998 : 13940 : if (VAR_P (decl) && CP_DECL_THREAD_LOCAL_P (decl))
999 : : return false;
1000 : 13937 : if (cxx_omp_predetermined_sharing (decl) != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
1001 : : return false;
1002 : : return true;
1003 : : }
1004 : :
1005 : : /* Note DECL use in OpenMP region OMP_CTX during genericization. */
1006 : :
1007 : : static void
1008 : 14124 : omp_cxx_notice_variable (struct cp_genericize_omp_taskreg *omp_ctx, tree decl)
1009 : : {
1010 : 14124 : splay_tree_node n = splay_tree_lookup (omp_ctx->variables,
1011 : : (splay_tree_key) decl);
1012 : 14124 : if (n == NULL)
1013 : : {
1014 : 4320 : int flags = OMP_CLAUSE_DEFAULT_SHARED;
1015 : 4320 : if (omp_ctx->outer)
1016 : 1257 : omp_cxx_notice_variable (omp_ctx->outer, decl);
1017 : 4320 : if (!omp_ctx->default_shared)
1018 : : {
1019 : 948 : struct cp_genericize_omp_taskreg *octx;
1020 : :
1021 : 1065 : for (octx = omp_ctx->outer; octx; octx = octx->outer)
1022 : : {
1023 : 902 : n = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
1024 : 902 : if (n && n->value != OMP_CLAUSE_DEFAULT_SHARED)
1025 : : {
1026 : : flags = OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
1027 : : break;
1028 : : }
1029 : 859 : if (octx->is_parallel)
1030 : : break;
1031 : : }
1032 : 948 : if (octx == NULL
1033 : 948 : && (TREE_CODE (decl) == PARM_DECL
1034 : 120 : || (!(TREE_STATIC (decl) || DECL_EXTERNAL (decl))
1035 : 44 : && DECL_CONTEXT (decl) == current_function_decl)))
1036 : : flags = OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
1037 : 861 : if (flags == OMP_CLAUSE_DEFAULT_FIRSTPRIVATE)
1038 : : {
1039 : : /* DECL is implicitly determined firstprivate in
1040 : : the current task construct. Ensure copy ctor and
1041 : : dtor are instantiated, because during gimplification
1042 : : it will be already too late. */
1043 : 130 : tree type = TREE_TYPE (decl);
1044 : 130 : if (is_invisiref_parm (decl))
1045 : 2 : type = TREE_TYPE (type);
1046 : 128 : else if (TYPE_REF_P (type))
1047 : 55 : type = TREE_TYPE (type);
1048 : 180 : while (TREE_CODE (type) == ARRAY_TYPE)
1049 : 50 : type = TREE_TYPE (type);
1050 : 130 : get_copy_ctor (type, tf_none);
1051 : 130 : get_dtor (type, tf_none);
1052 : : }
1053 : : }
1054 : 4320 : splay_tree_insert (omp_ctx->variables, (splay_tree_key) decl, flags);
1055 : : }
1056 : 14124 : }
1057 : :
1058 : : /* True if any of the element initializers in CTOR are TARGET_EXPRs that are
1059 : : not expected to elide, e.g. because unsafe_copy_elision_p is true. */
1060 : :
1061 : : static bool
1062 : 74817 : any_non_eliding_target_exprs (tree ctor)
1063 : : {
1064 : 240358 : for (const constructor_elt &e : *CONSTRUCTOR_ELTS (ctor))
1065 : : {
1066 : 165544 : if (TREE_CODE (e.value) == TARGET_EXPR
1067 : 165544 : && !TARGET_EXPR_ELIDING_P (e.value))
1068 : : return true;
1069 : : }
1070 : : return false;
1071 : : }
1072 : :
1073 : : /* If we might need to clean up a partially constructed object, break down the
1074 : : CONSTRUCTOR with split_nonconstant_init. Also expand VEC_INIT_EXPR at this
1075 : : point. If initializing TO with FROM is non-trivial, overwrite *REPLACE with
1076 : : the result. */
1077 : :
1078 : : static void
1079 : 65965421 : cp_genericize_init (tree *replace, tree from, tree to, vec<tree,va_gc>** flags)
1080 : : {
1081 : 65965421 : tree init = NULL_TREE;
1082 : 65965421 : if (TREE_CODE (from) == VEC_INIT_EXPR)
1083 : 1039 : init = expand_vec_init_expr (to, from, tf_warning_or_error, flags);
1084 : 65964382 : else if (TREE_CODE (from) == CONSTRUCTOR
1085 : 3085351 : && TREE_SIDE_EFFECTS (from)
1086 : 66040735 : && ((flag_exceptions
1087 : 76311 : && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (from)))
1088 : 74817 : || any_non_eliding_target_exprs (from)))
1089 : : {
1090 : 1539 : to = cp_stabilize_reference (to);
1091 : 1539 : replace_placeholders (from, to);
1092 : 1539 : init = split_nonconstant_init (to, from);
1093 : : }
1094 : :
1095 : 2578 : if (init)
1096 : : {
1097 : 2578 : if (*replace == from)
1098 : : /* Make cp_gimplify_init_expr call replace_decl on this
1099 : : TARGET_EXPR_INITIAL. */
1100 : 445 : init = fold_convert (void_type_node, init);
1101 : 2578 : *replace = init;
1102 : : }
1103 : 65965421 : }
1104 : :
1105 : : /* For an INIT_EXPR, replace the INIT_EXPR itself. */
1106 : :
1107 : : static void
1108 : 50743573 : cp_genericize_init_expr (tree *stmt_p)
1109 : : {
1110 : 50743573 : iloc_sentinel ils = EXPR_LOCATION (*stmt_p);
1111 : 50743573 : tree to = TREE_OPERAND (*stmt_p, 0);
1112 : 50743573 : tree from = TREE_OPERAND (*stmt_p, 1);
1113 : 5558140 : if (SIMPLE_TARGET_EXPR_P (from)
1114 : : /* Return gets confused if we clobber its INIT_EXPR this soon. */
1115 : 54797676 : && TREE_CODE (to) != RESULT_DECL)
1116 : 82841 : from = TARGET_EXPR_INITIAL (from);
1117 : 50743573 : cp_genericize_init (stmt_p, from, to, nullptr);
1118 : 50743573 : }
1119 : :
1120 : : /* For a TARGET_EXPR, change the TARGET_EXPR_INITIAL. We will need to use
1121 : : replace_decl later when we know what we're initializing. */
1122 : :
1123 : : static void
1124 : 15221848 : cp_genericize_target_expr (tree *stmt_p)
1125 : : {
1126 : 15221848 : iloc_sentinel ils = EXPR_LOCATION (*stmt_p);
1127 : 15221848 : tree slot = TARGET_EXPR_SLOT (*stmt_p);
1128 : 15221848 : vec<tree, va_gc> *flags = make_tree_vector ();
1129 : 15221848 : cp_genericize_init (&TARGET_EXPR_INITIAL (*stmt_p),
1130 : 15221848 : TARGET_EXPR_INITIAL (*stmt_p), slot, &flags);
1131 : 15221848 : gcc_assert (!DECL_INITIAL (slot));
1132 : 45665576 : for (tree f : flags)
1133 : : {
1134 : : /* Once initialization is complete TARGET_EXPR_CLEANUP becomes active, so
1135 : : disable any subobject cleanups. */
1136 : 32 : tree d = build_disable_temp_cleanup (f);
1137 : 32 : auto &r = TARGET_EXPR_INITIAL (*stmt_p);
1138 : 32 : r = add_stmt_to_compound (r, d);
1139 : : }
1140 : 15221848 : release_tree_vector (flags);
1141 : 15221848 : }
1142 : :
1143 : : /* Similar to if (target_expr_needs_replace) replace_decl, but TP is the
1144 : : TARGET_EXPR_INITIAL, and this also updates *_SLOT. We need this extra
1145 : : replacement when cp_folding TARGET_EXPR to preserve the invariant that
1146 : : AGGR_INIT_EXPR_SLOT agrees with the enclosing TARGET_EXPR_SLOT. */
1147 : :
1148 : : static bool
1149 : 137 : maybe_replace_decl (tree *tp, tree decl, tree replacement)
1150 : : {
1151 : 137 : if (!*tp || !VOID_TYPE_P (TREE_TYPE (*tp)))
1152 : : return false;
1153 : : tree t = *tp;
1154 : 69 : while (TREE_CODE (t) == COMPOUND_EXPR)
1155 : 0 : t = TREE_OPERAND (t, 1);
1156 : 69 : if (TREE_CODE (t) == AGGR_INIT_EXPR)
1157 : 69 : replace_decl (&AGGR_INIT_EXPR_SLOT (t), decl, replacement);
1158 : 0 : else if (TREE_CODE (t) == VEC_INIT_EXPR)
1159 : 0 : replace_decl (&VEC_INIT_EXPR_SLOT (t), decl, replacement);
1160 : : else
1161 : 0 : replace_decl (tp, decl, replacement);
1162 : : return true;
1163 : : }
1164 : :
1165 : : /* Genericization context. */
1166 : :
1167 : 42090493 : struct cp_genericize_data
1168 : : {
1169 : : hash_set<tree> *p_set;
1170 : : auto_vec<tree> bind_expr_stack;
1171 : : struct cp_genericize_omp_taskreg *omp_ctx;
1172 : : tree try_block;
1173 : : bool no_sanitize_p;
1174 : : bool handle_invisiref_parm_p;
1175 : : };
1176 : :
1177 : : /* Emit an error about taking the address of an immediate function.
1178 : : EXPR is the whole expression; DECL is the immediate function. */
1179 : :
1180 : : static void
1181 : 63 : taking_address_of_imm_fn_error (tree expr, tree decl)
1182 : : {
1183 : 63 : auto_diagnostic_group d;
1184 : 63 : const location_t loc = (TREE_CODE (expr) == PTRMEM_CST
1185 : 63 : ? PTRMEM_CST_LOCATION (expr)
1186 : 63 : : EXPR_LOCATION (expr));
1187 : 63 : error_at (loc, "taking address of an immediate function %qD", decl);
1188 : 63 : maybe_explain_promoted_consteval (loc, decl);
1189 : 63 : }
1190 : :
1191 : : /* Build up an INIT_EXPR to initialize the object of a constructor call that
1192 : : has been folded to a constant value. CALL is the CALL_EXPR for the
1193 : : constructor call; INIT is the value. */
1194 : :
1195 : : static tree
1196 : 294 : cp_build_init_expr_for_ctor (tree call, tree init)
1197 : : {
1198 : 294 : tree a = CALL_EXPR_ARG (call, 0);
1199 : 294 : if (is_dummy_object (a))
1200 : : return init;
1201 : 294 : const bool return_this = targetm.cxx.cdtor_returns_this ();
1202 : 294 : const location_t loc = EXPR_LOCATION (call);
1203 : 294 : if (return_this)
1204 : 0 : a = cp_save_expr (a);
1205 : 294 : tree s = build_fold_indirect_ref_loc (loc, a);
1206 : 294 : init = cp_build_init_expr (s, init);
1207 : 294 : if (return_this)
1208 : : {
1209 : 0 : init = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (call), init,
1210 : 0 : fold_convert_loc (loc, TREE_TYPE (call), a));
1211 : 0 : suppress_warning (init);
1212 : : }
1213 : : return init;
1214 : : }
1215 : :
1216 : : /* A walk_tree callback for cp_fold_function and cp_fully_fold_init to handle
1217 : : immediate functions. */
1218 : :
1219 : : static tree
1220 : 1143408704 : cp_fold_immediate_r (tree *stmt_p, int *walk_subtrees, void *data_)
1221 : : {
1222 : 1143408704 : auto data = static_cast<cp_fold_data *>(data_);
1223 : 1143408704 : tree stmt = *stmt_p;
1224 : : /* The purpose of this is not to emit errors for mce_unknown. */
1225 : 1143408704 : const tsubst_flags_t complain = (data->flags & ff_mce_false
1226 : 1143408704 : ? tf_error : tf_none);
1227 : 1143408704 : const tree_code code = TREE_CODE (stmt);
1228 : :
1229 : : /* No need to look into types or unevaluated operands.
1230 : : NB: This affects cp_fold_r as well. */
1231 : 1143408704 : if (TYPE_P (stmt)
1232 : 1141970242 : || unevaluated_p (code)
1233 : : /* We do not use in_immediate_context here because it checks
1234 : : more than is desirable, e.g., sk_template_parms. */
1235 : 1141561004 : || cp_unevaluated_operand
1236 : 2284969708 : || (current_function_decl
1237 : 2225524212 : && DECL_IMMEDIATE_FUNCTION_P (current_function_decl)))
1238 : : {
1239 : 1854943 : *walk_subtrees = 0;
1240 : 1854943 : return NULL_TREE;
1241 : : }
1242 : :
1243 : 1141553761 : tree decl = NULL_TREE;
1244 : 1141553761 : bool call_p = false;
1245 : :
1246 : : /* We are looking for &fn or fn(). */
1247 : 1141553761 : switch (code)
1248 : : {
1249 : 54254877 : case CALL_EXPR:
1250 : 54254877 : case AGGR_INIT_EXPR:
1251 : 54254877 : if (tree fn = cp_get_callee (stmt))
1252 : 54040848 : if (TREE_CODE (fn) != ADDR_EXPR || ADDR_EXPR_DENOTES_CALL_P (fn))
1253 : 52166220 : decl = cp_get_fndecl_from_callee (fn, /*fold*/false);
1254 : 52166220 : call_p = true;
1255 : 52166220 : break;
1256 : 9034 : case PTRMEM_CST:
1257 : 9034 : decl = PTRMEM_CST_MEMBER (stmt);
1258 : 9034 : break;
1259 : 86144373 : case ADDR_EXPR:
1260 : 86144373 : if (!ADDR_EXPR_DENOTES_CALL_P (stmt))
1261 : 34605406 : decl = TREE_OPERAND (stmt, 0);
1262 : : break;
1263 : 8391092 : case IF_STMT:
1264 : 8391092 : if (IF_STMT_CONSTEVAL_P (stmt))
1265 : : {
1266 : 17611 : if (!data->pset.add (stmt))
1267 : 17611 : cp_walk_tree (&ELSE_CLAUSE (stmt), cp_fold_immediate_r, data_,
1268 : : NULL);
1269 : 17611 : *walk_subtrees = 0;
1270 : 17611 : return NULL_TREE;
1271 : : }
1272 : : /* FALLTHRU */
1273 : 1001127866 : default:
1274 : 1001127866 : if (data->pset.add (stmt))
1275 : 183920818 : *walk_subtrees = 0;
1276 : : return NULL_TREE;
1277 : : }
1278 : :
1279 : 86780660 : if (!decl || TREE_CODE (decl) != FUNCTION_DECL)
1280 : 86543768 : return NULL_TREE;
1281 : :
1282 : : /* Fully escalate once all templates have been instantiated. What we're
1283 : : calling is not a consteval function but it may become one. This
1284 : : requires recursing; DECL may be promoted to consteval because it
1285 : : contains an escalating expression E, but E itself may have to be
1286 : : promoted first, etc. */
1287 : 53864516 : if (at_eof > 1 && unchecked_immediate_escalating_function_p (decl))
1288 : : {
1289 : : /* Set before the actual walk to avoid endless recursion. */
1290 : 2037070 : DECL_ESCALATION_CHECKED_P (decl) = true;
1291 : : /* We're only looking for the first escalating expression. Let us not
1292 : : walk more trees than necessary, hence mce_unknown. */
1293 : 2037070 : cp_fold_immediate (&DECL_SAVED_TREE (decl), mce_unknown, decl);
1294 : : }
1295 : :
1296 : : /* [expr.const]p16 "An expression or conversion is immediate-escalating if
1297 : : it is not initially in an immediate function context and it is either
1298 : : -- an immediate invocation that is not a constant expression and is not
1299 : : a subexpression of an immediate invocation."
1300 : :
1301 : : If we are in an immediate-escalating function, the immediate-escalating
1302 : : expression or conversion makes it an immediate function. So STMT does
1303 : : not need to produce a constant expression. */
1304 : 107729032 : if (DECL_IMMEDIATE_FUNCTION_P (decl))
1305 : : {
1306 : 210144 : tree e = cxx_constant_value (stmt, tf_none);
1307 : 210144 : if (e == error_mark_node)
1308 : : {
1309 : : /* This takes care of, e.g.,
1310 : : template <typename T>
1311 : : constexpr int f(T t)
1312 : : {
1313 : : return id(t);
1314 : : }
1315 : : where id (consteval) causes f<int> to be promoted. */
1316 : 1002 : if (immediate_escalating_function_p (current_function_decl))
1317 : 325 : promote_function_to_consteval (current_function_decl);
1318 : 677 : else if (complain & tf_error)
1319 : : {
1320 : 569 : if (call_p)
1321 : : {
1322 : 518 : auto_diagnostic_group d;
1323 : 518 : location_t loc = cp_expr_loc_or_input_loc (stmt);
1324 : 518 : error_at (loc, "call to consteval function %qE is "
1325 : : "not a constant expression", stmt);
1326 : : /* Explain why it's not a constant expression. */
1327 : 518 : *stmt_p = cxx_constant_value (stmt, complain);
1328 : 518 : maybe_explain_promoted_consteval (loc, decl);
1329 : 518 : }
1330 : 51 : else if (!data->pset.add (stmt))
1331 : : {
1332 : 51 : taking_address_of_imm_fn_error (stmt, decl);
1333 : 51 : *stmt_p = build_zero_cst (TREE_TYPE (stmt));
1334 : : }
1335 : : /* If we're giving hard errors, continue the walk rather than
1336 : : bailing out after the first error. */
1337 : 569 : return NULL_TREE;
1338 : : }
1339 : 433 : *walk_subtrees = 0;
1340 : 433 : return stmt;
1341 : : }
1342 : : /* We've evaluated the consteval function call. */
1343 : 209142 : if (call_p)
1344 : : {
1345 : 306218 : if (code == CALL_EXPR && DECL_CONSTRUCTOR_P (decl))
1346 : 0 : *stmt_p = cp_build_init_expr_for_ctor (stmt, e);
1347 : : else
1348 : 209142 : *stmt_p = e;
1349 : : }
1350 : : }
1351 : : /* We've encountered a function call that may turn out to be consteval
1352 : : later. Store its caller so that we can ensure that the call is
1353 : : a constant expression. */
1354 : 53654372 : else if (unchecked_immediate_escalating_function_p (decl))
1355 : : {
1356 : : /* Make sure we're not inserting new elements while walking
1357 : : the deferred_escalating_exprs hash table; if we are, it's
1358 : : likely that a function wasn't properly marked checked for
1359 : : i-e expressions. */
1360 : 9014815 : gcc_checking_assert (at_eof <= 1);
1361 : 9014815 : if (current_function_decl)
1362 : 9014512 : remember_escalating_expr (current_function_decl);
1363 : : /* auto p = &f<int>; in the global scope won't be ensconced in
1364 : : a function we could store for later at this point. (If there's
1365 : : no c_f_d at this point and we're dealing with a call, we should
1366 : : see the call when cp_fold_function __static_i_and_d.) */
1367 : 303 : else if (!call_p)
1368 : 85 : remember_escalating_expr (stmt);
1369 : : }
1370 : :
1371 : : return NULL_TREE;
1372 : : }
1373 : :
1374 : : /* Perform any pre-gimplification folding of C++ front end trees to
1375 : : GENERIC.
1376 : : Note: The folding of non-omp cases is something to move into
1377 : : the middle-end. As for now we have most foldings only on GENERIC
1378 : : in fold-const, we need to perform this before transformation to
1379 : : GIMPLE-form.
1380 : :
1381 : : ??? This is algorithmically weird because walk_tree works in pre-order, so
1382 : : we see outer expressions before inner expressions. This isn't as much of an
1383 : : issue because cp_fold recurses into subexpressions in many cases, but then
1384 : : walk_tree walks back into those subexpressions again. We avoid the
1385 : : resulting complexity problem by caching the result of cp_fold, but it's
1386 : : inelegant. */
1387 : :
1388 : : static tree
1389 : 1881106863 : cp_fold_r (tree *stmt_p, int *walk_subtrees, void *data_)
1390 : : {
1391 : 1881106863 : cp_fold_data *data = (cp_fold_data*)data_;
1392 : 1881106863 : tree stmt = *stmt_p;
1393 : 1881106863 : enum tree_code code = TREE_CODE (stmt);
1394 : :
1395 : 1881106863 : *stmt_p = stmt = cp_fold (*stmt_p, data->flags);
1396 : :
1397 : 1881106863 : if (data->pset.add (stmt))
1398 : : {
1399 : : /* Don't walk subtrees of stmts we've already walked once, otherwise
1400 : : we can have exponential complexity with e.g. lots of nested
1401 : : SAVE_EXPRs or TARGET_EXPRs. cp_fold uses a cache and will return
1402 : : always the same tree, which the first time cp_fold_r has been
1403 : : called on it had the subtrees walked. */
1404 : 285964619 : *walk_subtrees = 0;
1405 : 285964619 : return NULL_TREE;
1406 : : }
1407 : :
1408 : 1595142244 : code = TREE_CODE (stmt);
1409 : 1595142244 : switch (code)
1410 : : {
1411 : 25919 : tree x;
1412 : 25919 : int i, n;
1413 : 25919 : case OMP_FOR:
1414 : 25919 : case OMP_SIMD:
1415 : 25919 : case OMP_DISTRIBUTE:
1416 : 25919 : case OMP_LOOP:
1417 : 25919 : case OMP_TASKLOOP:
1418 : 25919 : case OMP_TILE:
1419 : 25919 : case OMP_UNROLL:
1420 : 25919 : case OACC_LOOP:
1421 : 25919 : cp_walk_tree (&OMP_FOR_BODY (stmt), cp_fold_r, data, NULL);
1422 : 25919 : cp_walk_tree (&OMP_FOR_CLAUSES (stmt), cp_fold_r, data, NULL);
1423 : 25919 : cp_walk_tree (&OMP_FOR_INIT (stmt), cp_fold_r, data, NULL);
1424 : 25919 : x = OMP_FOR_COND (stmt);
1425 : 25919 : if (x && TREE_CODE_CLASS (TREE_CODE (x)) == tcc_comparison)
1426 : : {
1427 : 0 : cp_walk_tree (&TREE_OPERAND (x, 0), cp_fold_r, data, NULL);
1428 : 0 : cp_walk_tree (&TREE_OPERAND (x, 1), cp_fold_r, data, NULL);
1429 : : }
1430 : 19096 : else if (x && TREE_CODE (x) == TREE_VEC)
1431 : : {
1432 : 19096 : n = TREE_VEC_LENGTH (x);
1433 : 43835 : for (i = 0; i < n; i++)
1434 : : {
1435 : 24739 : tree o = TREE_VEC_ELT (x, i);
1436 : 24739 : if (o && TREE_CODE_CLASS (TREE_CODE (o)) == tcc_comparison)
1437 : 23826 : cp_walk_tree (&TREE_OPERAND (o, 1), cp_fold_r, data, NULL);
1438 : : }
1439 : : }
1440 : 25919 : x = OMP_FOR_INCR (stmt);
1441 : 25919 : if (x && TREE_CODE (x) == TREE_VEC)
1442 : : {
1443 : 19096 : n = TREE_VEC_LENGTH (x);
1444 : 43835 : for (i = 0; i < n; i++)
1445 : : {
1446 : 24739 : tree o = TREE_VEC_ELT (x, i);
1447 : 24739 : if (o && TREE_CODE (o) == MODIFY_EXPR)
1448 : 6010 : o = TREE_OPERAND (o, 1);
1449 : 23826 : if (o && (TREE_CODE (o) == PLUS_EXPR || TREE_CODE (o) == MINUS_EXPR
1450 : 19263 : || TREE_CODE (o) == POINTER_PLUS_EXPR))
1451 : : {
1452 : 6010 : cp_walk_tree (&TREE_OPERAND (o, 0), cp_fold_r, data, NULL);
1453 : 6010 : cp_walk_tree (&TREE_OPERAND (o, 1), cp_fold_r, data, NULL);
1454 : : }
1455 : : }
1456 : : }
1457 : 25919 : cp_walk_tree (&OMP_FOR_PRE_BODY (stmt), cp_fold_r, data, NULL);
1458 : 25919 : *walk_subtrees = 0;
1459 : 25919 : return NULL_TREE;
1460 : :
1461 : 17409761 : case IF_STMT:
1462 : 17409761 : if (IF_STMT_CONSTEVAL_P (stmt))
1463 : : {
1464 : : /* Don't walk THEN_CLAUSE (stmt) for consteval if. IF_COND is always
1465 : : boolean_false_node. */
1466 : 17608 : cp_walk_tree (&ELSE_CLAUSE (stmt), cp_fold_r, data, NULL);
1467 : 17608 : cp_walk_tree (&IF_SCOPE (stmt), cp_fold_r, data, NULL);
1468 : 17608 : *walk_subtrees = 0;
1469 : 17608 : return NULL_TREE;
1470 : : }
1471 : : break;
1472 : :
1473 : : /* cp_genericize_{init,target}_expr are only for genericize time; they're
1474 : : here rather than in cp_genericize to avoid problems with the invisible
1475 : : reference transition. */
1476 : 50758860 : case INIT_EXPR:
1477 : 50758860 : if (data->flags & ff_genericize)
1478 : 50743573 : cp_genericize_init_expr (stmt_p);
1479 : : break;
1480 : :
1481 : 15714725 : case TARGET_EXPR:
1482 : 15714725 : if (!flag_no_inline)
1483 : 15133508 : if (tree &init = TARGET_EXPR_INITIAL (stmt))
1484 : : {
1485 : 15133508 : tree folded = maybe_constant_init (init, TARGET_EXPR_SLOT (stmt),
1486 : 15133508 : (data->flags & ff_mce_false
1487 : : ? mce_false : mce_unknown));
1488 : 15133508 : if (folded != init && TREE_CONSTANT (folded))
1489 : 896532 : init = folded;
1490 : : }
1491 : :
1492 : : /* This needs to happen between the constexpr evaluation (which wants
1493 : : pre-generic trees) and fold (which wants the cp_genericize_init
1494 : : transformations). */
1495 : 15714725 : if (data->flags & ff_genericize)
1496 : 15221848 : cp_genericize_target_expr (stmt_p);
1497 : :
1498 : 15714725 : if (tree &init = TARGET_EXPR_INITIAL (stmt))
1499 : : {
1500 : 15714725 : cp_walk_tree (&init, cp_fold_r, data, NULL);
1501 : 15714725 : cp_walk_tree (&TARGET_EXPR_CLEANUP (stmt), cp_fold_r, data, NULL);
1502 : 15714725 : *walk_subtrees = 0;
1503 : : /* Folding might replace e.g. a COND_EXPR with a TARGET_EXPR; in
1504 : : that case, strip it in favor of this one. */
1505 : 15714725 : if (TREE_CODE (init) == TARGET_EXPR)
1506 : : {
1507 : 137 : tree sub = TARGET_EXPR_INITIAL (init);
1508 : 137 : maybe_replace_decl (&sub, TARGET_EXPR_SLOT (init),
1509 : 137 : TARGET_EXPR_SLOT (stmt));
1510 : 137 : init = sub;
1511 : : }
1512 : : }
1513 : : break;
1514 : :
1515 : : default:
1516 : : break;
1517 : : }
1518 : :
1519 : : return NULL_TREE;
1520 : : }
1521 : :
1522 : : /* Fold ALL the trees! FIXME we should be able to remove this, but
1523 : : apparently that still causes optimization regressions. */
1524 : :
1525 : : void
1526 : 55881856 : cp_fold_function (tree fndecl)
1527 : : {
1528 : : /* By now all manifestly-constant-evaluated expressions will have
1529 : : been constant-evaluated already if possible, so we can safely
1530 : : pass ff_mce_false. */
1531 : 55881856 : cp_fold_data data (ff_genericize | ff_mce_false);
1532 : : /* Do cp_fold_immediate_r in separate whole IL walk instead of during
1533 : : cp_fold_r, as otherwise expressions using results of immediate functions
1534 : : might not be folded as cp_fold is called on those before cp_fold_r is
1535 : : called on their argument. */
1536 : 55881856 : if (cxx_dialect >= cxx20)
1537 : : {
1538 : 19801007 : cp_walk_tree (&DECL_SAVED_TREE (fndecl), cp_fold_immediate_r,
1539 : : &data, NULL);
1540 : 19801007 : data.pset.empty ();
1541 : : }
1542 : 55881856 : cp_walk_tree (&DECL_SAVED_TREE (fndecl), cp_fold_r, &data, NULL);
1543 : :
1544 : : /* This is merely an optimization: if FNDECL has no i-e expressions,
1545 : : we'll not save c_f_d, and we can safely say that FNDECL will not
1546 : : be promoted to consteval. */
1547 : 55881856 : if (deferred_escalating_exprs
1548 : 55881856 : && !deferred_escalating_exprs->contains (current_function_decl))
1549 : 13970869 : DECL_ESCALATION_CHECKED_P (fndecl) = true;
1550 : 55881856 : }
1551 : :
1552 : : /* We've stashed immediate-escalating functions. Now see if they indeed
1553 : : ought to be promoted to consteval. */
1554 : :
1555 : : void
1556 : 93654 : process_and_check_pending_immediate_escalating_fns ()
1557 : : {
1558 : : /* This will be null for -fno-immediate-escalation. */
1559 : 93654 : if (!deferred_escalating_exprs)
1560 : : return;
1561 : :
1562 : 8940040 : for (auto e : *deferred_escalating_exprs)
1563 : 4466969 : if (TREE_CODE (e) == FUNCTION_DECL && !DECL_ESCALATION_CHECKED_P (e))
1564 : 2972819 : cp_fold_immediate (&DECL_SAVED_TREE (e), mce_false, e);
1565 : :
1566 : : /* We've escalated every function that could have been promoted to
1567 : : consteval. Check that we are not taking the address of a consteval
1568 : : function. */
1569 : 8940125 : for (auto e : *deferred_escalating_exprs)
1570 : : {
1571 : 4466969 : if (TREE_CODE (e) == FUNCTION_DECL)
1572 : 4466884 : continue;
1573 : 85 : tree decl = (TREE_CODE (e) == PTRMEM_CST
1574 : 85 : ? PTRMEM_CST_MEMBER (e)
1575 : 85 : : TREE_OPERAND (e, 0));
1576 : 170 : if (DECL_IMMEDIATE_FUNCTION_P (decl))
1577 : 12 : taking_address_of_imm_fn_error (e, decl);
1578 : : }
1579 : :
1580 : 6187 : deferred_escalating_exprs = nullptr;
1581 : : }
1582 : :
1583 : : /* Turn SPACESHIP_EXPR EXPR into GENERIC. */
1584 : :
1585 : 64714 : static tree genericize_spaceship (tree expr)
1586 : : {
1587 : 64714 : iloc_sentinel s (cp_expr_location (expr));
1588 : 64714 : tree type = TREE_TYPE (expr);
1589 : 64714 : tree op0 = TREE_OPERAND (expr, 0);
1590 : 64714 : tree op1 = TREE_OPERAND (expr, 1);
1591 : 64714 : return genericize_spaceship (input_location, type, op0, op1);
1592 : 64714 : }
1593 : :
1594 : : /* If EXPR involves an anonymous VLA type, prepend a DECL_EXPR for that type
1595 : : to trigger gimplify_type_sizes; otherwise a cast to pointer-to-VLA confuses
1596 : : the middle-end (c++/88256). If EXPR is a DECL, use add_stmt and return
1597 : : NULL_TREE; otherwise return a COMPOUND_STMT of the DECL_EXPR and EXPR. */
1598 : :
1599 : : tree
1600 : 118804269 : predeclare_vla (tree expr)
1601 : : {
1602 : 118804269 : tree type = TREE_TYPE (expr);
1603 : 118804269 : if (type == error_mark_node)
1604 : : return expr;
1605 : 118804186 : if (is_typedef_decl (expr))
1606 : 118804186 : type = DECL_ORIGINAL_TYPE (expr);
1607 : :
1608 : : /* We need to strip pointers for gimplify_type_sizes. */
1609 : 118804186 : tree vla = type;
1610 : 177235228 : while (POINTER_TYPE_P (vla))
1611 : : {
1612 : 60720818 : if (TYPE_NAME (vla))
1613 : : return expr;
1614 : 58431042 : vla = TREE_TYPE (vla);
1615 : : }
1616 : 56810193 : if (vla == type || TYPE_NAME (vla)
1617 : 117040431 : || !variably_modified_type_p (vla, NULL_TREE))
1618 : 116514271 : return expr;
1619 : :
1620 : 139 : tree decl = build_decl (input_location, TYPE_DECL, NULL_TREE, vla);
1621 : 139 : DECL_ARTIFICIAL (decl) = 1;
1622 : 139 : TYPE_NAME (vla) = decl;
1623 : 139 : tree dexp = build_stmt (input_location, DECL_EXPR, decl);
1624 : 139 : if (DECL_P (expr))
1625 : : {
1626 : 5 : add_stmt (dexp);
1627 : 5 : return NULL_TREE;
1628 : : }
1629 : : else
1630 : : {
1631 : 134 : expr = build2 (COMPOUND_EXPR, type, dexp, expr);
1632 : 134 : return expr;
1633 : : }
1634 : : }
1635 : :
1636 : : /* Perform any pre-gimplification lowering of C++ front end trees to
1637 : : GENERIC. */
1638 : :
1639 : : static tree
1640 : 1521887890 : cp_genericize_r (tree *stmt_p, int *walk_subtrees, void *data)
1641 : : {
1642 : 1539298858 : tree stmt = *stmt_p;
1643 : 1539298858 : struct cp_genericize_data *wtd = (struct cp_genericize_data *) data;
1644 : 1539298858 : hash_set<tree> *p_set = wtd->p_set;
1645 : :
1646 : : /* If in an OpenMP context, note var uses. */
1647 : 1539298858 : if (UNLIKELY (wtd->omp_ctx != NULL)
1648 : 583225 : && (VAR_P (stmt)
1649 : : || TREE_CODE (stmt) == PARM_DECL
1650 : : || TREE_CODE (stmt) == RESULT_DECL)
1651 : 1539406436 : && omp_var_to_track (stmt))
1652 : 12364 : omp_cxx_notice_variable (wtd->omp_ctx, stmt);
1653 : :
1654 : : /* Don't dereference parms in a thunk, pass the references through. */
1655 : 64301341 : if ((TREE_CODE (stmt) == CALL_EXPR && call_from_lambda_thunk_p (stmt))
1656 : 1603563482 : || (TREE_CODE (stmt) == AGGR_INIT_EXPR && AGGR_INIT_FROM_THUNK_P (stmt)))
1657 : : {
1658 : 36907 : *walk_subtrees = 0;
1659 : 36907 : return NULL;
1660 : : }
1661 : :
1662 : : /* Dereference invisible reference parms. */
1663 : 1539261951 : if (wtd->handle_invisiref_parm_p && is_invisiref_parm (stmt))
1664 : : {
1665 : 1162379 : *stmt_p = convert_from_reference (stmt);
1666 : 1162379 : p_set->add (*stmt_p);
1667 : 1162379 : *walk_subtrees = 0;
1668 : 1162379 : return NULL;
1669 : : }
1670 : :
1671 : : /* Map block scope extern declarations to visible declarations with the
1672 : : same name and type in outer scopes if any. */
1673 : 1538099572 : if (VAR_OR_FUNCTION_DECL_P (stmt) && DECL_LOCAL_DECL_P (stmt))
1674 : 20519 : if (tree alias = DECL_LOCAL_DECL_ALIAS (stmt))
1675 : : {
1676 : 20519 : if (alias != error_mark_node)
1677 : : {
1678 : 20516 : *stmt_p = alias;
1679 : 20516 : TREE_USED (alias) |= TREE_USED (stmt);
1680 : : }
1681 : 20519 : *walk_subtrees = 0;
1682 : 20519 : return NULL;
1683 : : }
1684 : :
1685 : 1538079053 : if (TREE_CODE (stmt) == INTEGER_CST
1686 : 185317140 : && TYPE_REF_P (TREE_TYPE (stmt))
1687 : 152 : && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))
1688 : 1538079054 : && !wtd->no_sanitize_p)
1689 : : {
1690 : 1 : ubsan_maybe_instrument_reference (stmt_p);
1691 : 1 : if (*stmt_p != stmt)
1692 : : {
1693 : 1 : *walk_subtrees = 0;
1694 : 1 : return NULL_TREE;
1695 : : }
1696 : : }
1697 : :
1698 : : /* Other than invisiref parms, don't walk the same tree twice. */
1699 : 1538079052 : if (p_set->contains (stmt))
1700 : : {
1701 : 281035688 : *walk_subtrees = 0;
1702 : 281035688 : return NULL_TREE;
1703 : : }
1704 : :
1705 : 1257043364 : switch (TREE_CODE (stmt))
1706 : : {
1707 : 100651245 : case ADDR_EXPR:
1708 : 100651245 : if (is_invisiref_parm (TREE_OPERAND (stmt, 0)))
1709 : : {
1710 : : /* If in an OpenMP context, note var uses. */
1711 : 540184 : if (UNLIKELY (wtd->omp_ctx != NULL)
1712 : 540184 : && omp_var_to_track (TREE_OPERAND (stmt, 0)))
1713 : 412 : omp_cxx_notice_variable (wtd->omp_ctx, TREE_OPERAND (stmt, 0));
1714 : 540184 : *stmt_p = fold_convert (TREE_TYPE (stmt), TREE_OPERAND (stmt, 0));
1715 : 540184 : *walk_subtrees = 0;
1716 : : }
1717 : : break;
1718 : :
1719 : 36392092 : case RETURN_EXPR:
1720 : 36392092 : if (TREE_OPERAND (stmt, 0))
1721 : : {
1722 : 35724093 : if (error_operand_p (TREE_OPERAND (stmt, 0))
1723 : 35724093 : && warn_return_type)
1724 : : /* Suppress -Wreturn-type for this function. */
1725 : 12 : suppress_warning (current_function_decl, OPT_Wreturn_type);
1726 : :
1727 : 35724093 : if (is_invisiref_parm (TREE_OPERAND (stmt, 0)))
1728 : : /* Don't dereference an invisiref RESULT_DECL inside a
1729 : : RETURN_EXPR. */
1730 : 600 : *walk_subtrees = 0;
1731 : 35724093 : if (RETURN_EXPR_LOCAL_ADDR_P (stmt))
1732 : : {
1733 : : /* Don't return the address of a local variable. */
1734 : 166 : tree *p = &TREE_OPERAND (stmt, 0);
1735 : 332 : while (TREE_CODE (*p) == COMPOUND_EXPR)
1736 : 0 : p = &TREE_OPERAND (*p, 0);
1737 : 166 : if (TREE_CODE (*p) == INIT_EXPR)
1738 : : {
1739 : 166 : tree op = TREE_OPERAND (*p, 1);
1740 : 166 : tree new_op = build2 (COMPOUND_EXPR, TREE_TYPE (op), op,
1741 : 166 : build_zero_cst (TREE_TYPE (op)));
1742 : 166 : TREE_OPERAND (*p, 1) = new_op;
1743 : : }
1744 : : }
1745 : : }
1746 : : break;
1747 : :
1748 : 82147 : case OMP_CLAUSE:
1749 : 82147 : switch (OMP_CLAUSE_CODE (stmt))
1750 : : {
1751 : 2581 : case OMP_CLAUSE_LASTPRIVATE:
1752 : : /* Don't dereference an invisiref in OpenMP clauses. */
1753 : 2581 : if (is_invisiref_parm (OMP_CLAUSE_DECL (stmt)))
1754 : : {
1755 : 53 : *walk_subtrees = 0;
1756 : 53 : if (OMP_CLAUSE_LASTPRIVATE_STMT (stmt))
1757 : 48 : cp_walk_tree (&OMP_CLAUSE_LASTPRIVATE_STMT (stmt),
1758 : : cp_genericize_r, data, NULL);
1759 : : }
1760 : : break;
1761 : 2043 : case OMP_CLAUSE_PRIVATE:
1762 : : /* Don't dereference an invisiref in OpenMP clauses. */
1763 : 2043 : if (is_invisiref_parm (OMP_CLAUSE_DECL (stmt)))
1764 : 8 : *walk_subtrees = 0;
1765 : 2035 : else if (wtd->omp_ctx != NULL)
1766 : : {
1767 : : /* Private clause doesn't cause any references to the
1768 : : var in outer contexts, avoid calling
1769 : : omp_cxx_notice_variable for it. */
1770 : 584 : struct cp_genericize_omp_taskreg *old = wtd->omp_ctx;
1771 : 584 : wtd->omp_ctx = NULL;
1772 : 584 : cp_walk_tree (&OMP_CLAUSE_DECL (stmt), cp_genericize_r,
1773 : : data, NULL);
1774 : 584 : wtd->omp_ctx = old;
1775 : 584 : *walk_subtrees = 0;
1776 : : }
1777 : : break;
1778 : 6008 : case OMP_CLAUSE_SHARED:
1779 : 6008 : case OMP_CLAUSE_FIRSTPRIVATE:
1780 : 6008 : case OMP_CLAUSE_COPYIN:
1781 : 6008 : case OMP_CLAUSE_COPYPRIVATE:
1782 : 6008 : case OMP_CLAUSE_INCLUSIVE:
1783 : 6008 : case OMP_CLAUSE_EXCLUSIVE:
1784 : : /* Don't dereference an invisiref in OpenMP clauses. */
1785 : 6008 : if (is_invisiref_parm (OMP_CLAUSE_DECL (stmt)))
1786 : 87 : *walk_subtrees = 0;
1787 : : break;
1788 : 8527 : case OMP_CLAUSE_REDUCTION:
1789 : 8527 : case OMP_CLAUSE_IN_REDUCTION:
1790 : 8527 : case OMP_CLAUSE_TASK_REDUCTION:
1791 : : /* Don't dereference an invisiref in reduction clause's
1792 : : OMP_CLAUSE_DECL either. OMP_CLAUSE_REDUCTION_{INIT,MERGE}
1793 : : still needs to be genericized. */
1794 : 8527 : if (is_invisiref_parm (OMP_CLAUSE_DECL (stmt)))
1795 : : {
1796 : 38 : *walk_subtrees = 0;
1797 : 38 : if (OMP_CLAUSE_REDUCTION_INIT (stmt))
1798 : 38 : cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (stmt),
1799 : : cp_genericize_r, data, NULL);
1800 : 38 : if (OMP_CLAUSE_REDUCTION_MERGE (stmt))
1801 : 38 : cp_walk_tree (&OMP_CLAUSE_REDUCTION_MERGE (stmt),
1802 : : cp_genericize_r, data, NULL);
1803 : : }
1804 : : break;
1805 : : default:
1806 : : break;
1807 : : }
1808 : : break;
1809 : :
1810 : : /* Due to the way voidify_wrapper_expr is written, we don't get a chance
1811 : : to lower this construct before scanning it, so we need to lower these
1812 : : before doing anything else. */
1813 : 5274832 : case CLEANUP_STMT:
1814 : 5274832 : *stmt_p = build2_loc (EXPR_LOCATION (stmt),
1815 : 5274832 : CLEANUP_EH_ONLY (stmt) ? TRY_CATCH_EXPR
1816 : : : TRY_FINALLY_EXPR,
1817 : : void_type_node,
1818 : 5274832 : CLEANUP_BODY (stmt),
1819 : 5274832 : CLEANUP_EXPR (stmt));
1820 : 5274832 : break;
1821 : :
1822 : 17410209 : case IF_STMT:
1823 : 17410209 : genericize_if_stmt (stmt_p);
1824 : : /* *stmt_p has changed, tail recurse to handle it again. */
1825 : 17410209 : return cp_genericize_r (stmt_p, walk_subtrees, data);
1826 : :
1827 : : /* COND_EXPR might have incompatible types in branches if one or both
1828 : : arms are bitfields. Fix it up now. */
1829 : 16515604 : case COND_EXPR:
1830 : 16515604 : {
1831 : 16515604 : tree type_left
1832 : 16515604 : = (TREE_OPERAND (stmt, 1)
1833 : 16515604 : ? is_bitfield_expr_with_lowered_type (TREE_OPERAND (stmt, 1))
1834 : : : NULL_TREE);
1835 : 16515604 : tree type_right
1836 : 16515604 : = (TREE_OPERAND (stmt, 2)
1837 : 16515604 : ? is_bitfield_expr_with_lowered_type (TREE_OPERAND (stmt, 2))
1838 : : : NULL_TREE);
1839 : 16515604 : if (type_left
1840 : 16515637 : && !useless_type_conversion_p (TREE_TYPE (stmt),
1841 : 33 : TREE_TYPE (TREE_OPERAND (stmt, 1))))
1842 : : {
1843 : 30 : TREE_OPERAND (stmt, 1)
1844 : 30 : = fold_convert (type_left, TREE_OPERAND (stmt, 1));
1845 : 30 : gcc_assert (useless_type_conversion_p (TREE_TYPE (stmt),
1846 : : type_left));
1847 : : }
1848 : 16515604 : if (type_right
1849 : 16515621 : && !useless_type_conversion_p (TREE_TYPE (stmt),
1850 : 17 : TREE_TYPE (TREE_OPERAND (stmt, 2))))
1851 : : {
1852 : 17 : TREE_OPERAND (stmt, 2)
1853 : 17 : = fold_convert (type_right, TREE_OPERAND (stmt, 2));
1854 : 17 : gcc_assert (useless_type_conversion_p (TREE_TYPE (stmt),
1855 : : type_right));
1856 : : }
1857 : : }
1858 : : break;
1859 : :
1860 : 22416387 : case BIND_EXPR:
1861 : 22416387 : if (UNLIKELY (wtd->omp_ctx != NULL))
1862 : : {
1863 : 26679 : tree decl;
1864 : 32954 : for (decl = BIND_EXPR_VARS (stmt); decl; decl = DECL_CHAIN (decl))
1865 : 6275 : if (VAR_P (decl)
1866 : 6227 : && !DECL_EXTERNAL (decl)
1867 : 12502 : && omp_var_to_track (decl))
1868 : : {
1869 : 588 : splay_tree_node n
1870 : 588 : = splay_tree_lookup (wtd->omp_ctx->variables,
1871 : : (splay_tree_key) decl);
1872 : 588 : if (n == NULL)
1873 : 588 : splay_tree_insert (wtd->omp_ctx->variables,
1874 : : (splay_tree_key) decl,
1875 : 588 : TREE_STATIC (decl)
1876 : : ? OMP_CLAUSE_DEFAULT_SHARED
1877 : : : OMP_CLAUSE_DEFAULT_PRIVATE);
1878 : : }
1879 : : }
1880 : 22416387 : if (sanitize_flags_p (SANITIZE_NULL | SANITIZE_ALIGNMENT | SANITIZE_VPTR))
1881 : : {
1882 : : /* The point here is to not sanitize static initializers. */
1883 : 3236 : bool no_sanitize_p = wtd->no_sanitize_p;
1884 : 3236 : wtd->no_sanitize_p = true;
1885 : 3236 : for (tree decl = BIND_EXPR_VARS (stmt);
1886 : 6332 : decl;
1887 : 3096 : decl = DECL_CHAIN (decl))
1888 : 3096 : if (VAR_P (decl)
1889 : 2695 : && TREE_STATIC (decl)
1890 : 3167 : && DECL_INITIAL (decl))
1891 : 12 : cp_walk_tree (&DECL_INITIAL (decl), cp_genericize_r, data, NULL);
1892 : 3236 : wtd->no_sanitize_p = no_sanitize_p;
1893 : : }
1894 : 22416387 : wtd->bind_expr_stack.safe_push (stmt);
1895 : 22416387 : cp_walk_tree (&BIND_EXPR_BODY (stmt),
1896 : : cp_genericize_r, data, NULL);
1897 : 22416387 : wtd->bind_expr_stack.pop ();
1898 : 22416387 : break;
1899 : :
1900 : 759 : case ASSERTION_STMT:
1901 : 759 : case PRECONDITION_STMT:
1902 : 759 : case POSTCONDITION_STMT:
1903 : 759 : {
1904 : 759 : if (tree check = build_contract_check (stmt))
1905 : : {
1906 : 759 : *stmt_p = check;
1907 : 759 : return cp_genericize_r (stmt_p, walk_subtrees, data);
1908 : : }
1909 : :
1910 : : /* If we didn't build a check, replace it with void_node so we don't
1911 : : leak contracts into GENERIC. */
1912 : 0 : *stmt_p = void_node;
1913 : 0 : *walk_subtrees = 0;
1914 : : }
1915 : 0 : break;
1916 : :
1917 : 28160 : case USING_STMT:
1918 : 28160 : {
1919 : 28160 : tree block = NULL_TREE;
1920 : :
1921 : : /* Get the innermost inclosing GIMPLE_BIND that has a non NULL
1922 : : BLOCK, and append an IMPORTED_DECL to its
1923 : : BLOCK_VARS chained list. */
1924 : 28160 : if (wtd->bind_expr_stack.exists ())
1925 : : {
1926 : 28160 : int i;
1927 : 28160 : for (i = wtd->bind_expr_stack.length () - 1; i >= 0; i--)
1928 : 28160 : if ((block = BIND_EXPR_BLOCK (wtd->bind_expr_stack[i])))
1929 : : break;
1930 : : }
1931 : 28160 : if (block)
1932 : : {
1933 : 28160 : tree decl = TREE_OPERAND (stmt, 0);
1934 : 28160 : gcc_assert (decl);
1935 : :
1936 : 28160 : if (undeduced_auto_decl (decl))
1937 : : /* Omit from the GENERIC, the back-end can't handle it. */;
1938 : : else
1939 : : {
1940 : 28157 : tree using_directive = make_node (IMPORTED_DECL);
1941 : 28157 : TREE_TYPE (using_directive) = void_type_node;
1942 : 28157 : DECL_CONTEXT (using_directive) = current_function_decl;
1943 : 56314 : DECL_SOURCE_LOCATION (using_directive)
1944 : 28157 : = cp_expr_loc_or_input_loc (stmt);
1945 : :
1946 : 28157 : IMPORTED_DECL_ASSOCIATED_DECL (using_directive) = decl;
1947 : 28157 : DECL_CHAIN (using_directive) = BLOCK_VARS (block);
1948 : 28157 : BLOCK_VARS (block) = using_directive;
1949 : : }
1950 : : }
1951 : : /* The USING_STMT won't appear in GENERIC. */
1952 : 28160 : *stmt_p = build1 (NOP_EXPR, void_type_node, integer_zero_node);
1953 : 28160 : *walk_subtrees = 0;
1954 : : }
1955 : 28160 : break;
1956 : :
1957 : 23724602 : case DECL_EXPR:
1958 : 23724602 : if (TREE_CODE (DECL_EXPR_DECL (stmt)) == USING_DECL)
1959 : : {
1960 : : /* Using decls inside DECL_EXPRs are just dropped on the floor. */
1961 : 14218 : *stmt_p = build1 (NOP_EXPR, void_type_node, integer_zero_node);
1962 : 14218 : *walk_subtrees = 0;
1963 : : }
1964 : : else
1965 : : {
1966 : 23710384 : tree d = DECL_EXPR_DECL (stmt);
1967 : 23710384 : if (VAR_P (d))
1968 : 47419590 : gcc_assert (CP_DECL_THREAD_LOCAL_P (d) == DECL_THREAD_LOCAL_P (d));
1969 : : }
1970 : : break;
1971 : :
1972 : 11592 : case OMP_PARALLEL:
1973 : 11592 : case OMP_TASK:
1974 : 11592 : case OMP_TASKLOOP:
1975 : 11592 : {
1976 : 11592 : struct cp_genericize_omp_taskreg omp_ctx;
1977 : 11592 : tree c, decl;
1978 : 11592 : splay_tree_node n;
1979 : :
1980 : 11592 : *walk_subtrees = 0;
1981 : 11592 : cp_walk_tree (&OMP_CLAUSES (stmt), cp_genericize_r, data, NULL);
1982 : 11592 : omp_ctx.is_parallel = TREE_CODE (stmt) == OMP_PARALLEL;
1983 : 11592 : omp_ctx.default_shared = omp_ctx.is_parallel;
1984 : 11592 : omp_ctx.outer = wtd->omp_ctx;
1985 : 11592 : omp_ctx.variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
1986 : 11592 : wtd->omp_ctx = &omp_ctx;
1987 : 27515 : for (c = OMP_CLAUSES (stmt); c; c = OMP_CLAUSE_CHAIN (c))
1988 : 15923 : switch (OMP_CLAUSE_CODE (c))
1989 : : {
1990 : 4756 : case OMP_CLAUSE_SHARED:
1991 : 4756 : case OMP_CLAUSE_PRIVATE:
1992 : 4756 : case OMP_CLAUSE_FIRSTPRIVATE:
1993 : 4756 : case OMP_CLAUSE_LASTPRIVATE:
1994 : 4756 : decl = OMP_CLAUSE_DECL (c);
1995 : 4756 : if (decl == error_mark_node || !omp_var_to_track (decl))
1996 : : break;
1997 : 519 : n = splay_tree_lookup (omp_ctx.variables, (splay_tree_key) decl);
1998 : 519 : if (n != NULL)
1999 : : break;
2000 : 1020 : splay_tree_insert (omp_ctx.variables, (splay_tree_key) decl,
2001 : 510 : OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
2002 : : ? OMP_CLAUSE_DEFAULT_SHARED
2003 : : : OMP_CLAUSE_DEFAULT_PRIVATE);
2004 : 510 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_PRIVATE && omp_ctx.outer)
2005 : 91 : omp_cxx_notice_variable (omp_ctx.outer, decl);
2006 : : break;
2007 : 1644 : case OMP_CLAUSE_DEFAULT:
2008 : 1644 : if (OMP_CLAUSE_DEFAULT_KIND (c) == OMP_CLAUSE_DEFAULT_SHARED)
2009 : 728 : omp_ctx.default_shared = true;
2010 : : default:
2011 : : break;
2012 : : }
2013 : 11592 : if (TREE_CODE (stmt) == OMP_TASKLOOP)
2014 : 995 : c_genericize_control_stmt (stmt_p, walk_subtrees, data,
2015 : : cp_genericize_r, cp_walk_subtrees);
2016 : : else
2017 : 10597 : cp_walk_tree (&OMP_BODY (stmt), cp_genericize_r, data, NULL);
2018 : 11592 : wtd->omp_ctx = omp_ctx.outer;
2019 : 11592 : splay_tree_delete (omp_ctx.variables);
2020 : : }
2021 : 11592 : break;
2022 : :
2023 : 6608 : case OMP_TARGET:
2024 : 6608 : cfun->has_omp_target = true;
2025 : 6608 : break;
2026 : :
2027 : 131697 : case TRY_BLOCK:
2028 : 131697 : {
2029 : 131697 : *walk_subtrees = 0;
2030 : 131697 : tree try_block = wtd->try_block;
2031 : 131697 : wtd->try_block = stmt;
2032 : 131697 : cp_walk_tree (&TRY_STMTS (stmt), cp_genericize_r, data, NULL);
2033 : 131697 : wtd->try_block = try_block;
2034 : 131697 : cp_walk_tree (&TRY_HANDLERS (stmt), cp_genericize_r, data, NULL);
2035 : : }
2036 : 131697 : break;
2037 : :
2038 : 18690611 : case MUST_NOT_THROW_EXPR:
2039 : : /* MUST_NOT_THROW_COND might be something else with TM. */
2040 : 18690611 : if (MUST_NOT_THROW_COND (stmt) == NULL_TREE)
2041 : : {
2042 : 18690593 : *walk_subtrees = 0;
2043 : 18690593 : tree try_block = wtd->try_block;
2044 : 18690593 : wtd->try_block = stmt;
2045 : 18690593 : cp_walk_tree (&TREE_OPERAND (stmt, 0), cp_genericize_r, data, NULL);
2046 : 18690593 : wtd->try_block = try_block;
2047 : : }
2048 : : break;
2049 : :
2050 : 124819 : case THROW_EXPR:
2051 : 124819 : {
2052 : 124819 : location_t loc = location_of (stmt);
2053 : 124819 : if (warning_suppressed_p (stmt /* What warning? */))
2054 : : /* Never mind. */;
2055 : 31059 : else if (wtd->try_block)
2056 : : {
2057 : 987 : if (TREE_CODE (wtd->try_block) == MUST_NOT_THROW_EXPR)
2058 : : {
2059 : 18 : auto_diagnostic_group d;
2060 : 31 : if (warning_at (loc, OPT_Wterminate,
2061 : : "%<throw%> will always call %<terminate%>")
2062 : 10 : && cxx_dialect >= cxx11
2063 : 36 : && DECL_DESTRUCTOR_P (current_function_decl))
2064 : 5 : inform (loc, "in C++11 destructors default to %<noexcept%>");
2065 : 18 : }
2066 : : }
2067 : : else
2068 : : {
2069 : 102 : if (warn_cxx11_compat && cxx_dialect < cxx11
2070 : 204 : && DECL_DESTRUCTOR_P (current_function_decl)
2071 : 1 : && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl))
2072 : : == NULL_TREE)
2073 : 30073 : && (get_defaulted_eh_spec (current_function_decl)
2074 : 1 : == empty_except_spec))
2075 : 1 : warning_at (loc, OPT_Wc__11_compat,
2076 : : "in C++11 this %<throw%> will call %<terminate%> "
2077 : : "because destructors default to %<noexcept%>");
2078 : : }
2079 : : }
2080 : : break;
2081 : :
2082 : 36075986 : case CONVERT_EXPR:
2083 : 36075986 : gcc_checking_assert (!AGGREGATE_TYPE_P (TREE_TYPE (stmt)));
2084 : 36075986 : gcc_assert (!CONVERT_EXPR_VBASE_PATH (stmt));
2085 : : break;
2086 : :
2087 : 64714 : case SPACESHIP_EXPR:
2088 : 64714 : *stmt_p = genericize_spaceship (*stmt_p);
2089 : 64714 : break;
2090 : :
2091 : 32952 : case PTRMEM_CST:
2092 : : /* By the time we get here we're handing off to the back end, so we don't
2093 : : need or want to preserve PTRMEM_CST anymore. */
2094 : 32952 : *stmt_p = cplus_expand_constant (stmt);
2095 : 32952 : *walk_subtrees = 0;
2096 : 32952 : break;
2097 : :
2098 : 94660 : case MEM_REF:
2099 : : /* For MEM_REF, make sure not to sanitize the second operand even
2100 : : if it has reference type. It is just an offset with a type
2101 : : holding other information. There is no other processing we
2102 : : need to do for INTEGER_CSTs, so just ignore the second argument
2103 : : unconditionally. */
2104 : 94660 : cp_walk_tree (&TREE_OPERAND (stmt, 0), cp_genericize_r, data, NULL);
2105 : 94660 : *walk_subtrees = 0;
2106 : 94660 : break;
2107 : :
2108 : 96711334 : case NOP_EXPR:
2109 : 96711334 : *stmt_p = predeclare_vla (*stmt_p);
2110 : :
2111 : : /* Warn of new allocations that are not big enough for the target
2112 : : type. */
2113 : 96711334 : if (warn_alloc_size
2114 : 973142 : && TREE_CODE (TREE_OPERAND (stmt, 0)) == CALL_EXPR
2115 : 96771831 : && POINTER_TYPE_P (TREE_TYPE (stmt)))
2116 : : {
2117 : 21419 : if (tree fndecl = get_callee_fndecl (TREE_OPERAND (stmt, 0)))
2118 : 21405 : if (DECL_IS_MALLOC (fndecl))
2119 : : {
2120 : 1202 : tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (fndecl));
2121 : 1202 : tree alloc_size = lookup_attribute ("alloc_size", attrs);
2122 : 1202 : if (alloc_size)
2123 : 1200 : warn_for_alloc_size (EXPR_LOCATION (stmt),
2124 : 1200 : TREE_TYPE (TREE_TYPE (stmt)),
2125 : 1200 : TREE_OPERAND (stmt, 0), alloc_size);
2126 : : }
2127 : : }
2128 : :
2129 : 96711334 : if (!wtd->no_sanitize_p
2130 : 96711329 : && sanitize_flags_p (SANITIZE_NULL | SANITIZE_ALIGNMENT)
2131 : 96725709 : && TYPE_REF_P (TREE_TYPE (stmt)))
2132 : 2182 : ubsan_maybe_instrument_reference (stmt_p);
2133 : : break;
2134 : :
2135 : 64257892 : case CALL_EXPR:
2136 : 64257892 : if (!wtd->no_sanitize_p
2137 : 64257892 : && sanitize_flags_p ((SANITIZE_NULL
2138 : : | SANITIZE_ALIGNMENT | SANITIZE_VPTR)))
2139 : : {
2140 : 14901 : tree fn = CALL_EXPR_FN (stmt);
2141 : 14901 : if (fn != NULL_TREE
2142 : 9270 : && !error_operand_p (fn)
2143 : 9270 : && INDIRECT_TYPE_P (TREE_TYPE (fn))
2144 : 24171 : && TREE_CODE (TREE_TYPE (TREE_TYPE (fn))) == METHOD_TYPE)
2145 : : {
2146 : 5238 : bool is_ctor
2147 : 5238 : = TREE_CODE (fn) == ADDR_EXPR
2148 : 5113 : && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
2149 : 15464 : && DECL_CONSTRUCTOR_P (TREE_OPERAND (fn, 0));
2150 : 5238 : if (sanitize_flags_p (SANITIZE_NULL | SANITIZE_ALIGNMENT))
2151 : 4627 : ubsan_maybe_instrument_member_call (stmt, is_ctor);
2152 : 5238 : if (sanitize_flags_p (SANITIZE_VPTR) && !is_ctor)
2153 : 4452 : cp_ubsan_maybe_instrument_member_call (stmt);
2154 : : }
2155 : 9663 : else if (fn == NULL_TREE
2156 : 5631 : && CALL_EXPR_IFN (stmt) == IFN_UBSAN_NULL
2157 : 4588 : && TREE_CODE (CALL_EXPR_ARG (stmt, 0)) == INTEGER_CST
2158 : 9669 : && TYPE_REF_P (TREE_TYPE (CALL_EXPR_ARG (stmt, 0))))
2159 : 6 : *walk_subtrees = 0;
2160 : : }
2161 : : /* Fall through. */
2162 : 68028295 : case AGGR_INIT_EXPR:
2163 : : /* For calls to a multi-versioned function, overload resolution
2164 : : returns the function with the highest target priority, that is,
2165 : : the version that will checked for dispatching first. If this
2166 : : version is inlinable, a direct call to this version can be made
2167 : : otherwise the call should go through the dispatcher. */
2168 : 68028295 : {
2169 : 68028295 : tree fn = cp_get_callee_fndecl_nofold (stmt);
2170 : 66882423 : if (fn && DECL_FUNCTION_VERSIONED (fn)
2171 : 68028427 : && (current_function_decl == NULL
2172 : 132 : || !targetm.target_option.can_inline_p (current_function_decl,
2173 : : fn)))
2174 : 120 : if (tree dis = get_function_version_dispatcher (fn))
2175 : : {
2176 : 120 : mark_versions_used (dis);
2177 : 120 : dis = build_address (dis);
2178 : 120 : if (TREE_CODE (stmt) == CALL_EXPR)
2179 : 117 : CALL_EXPR_FN (stmt) = dis;
2180 : : else
2181 : 3 : AGGR_INIT_EXPR_FN (stmt) = dis;
2182 : : }
2183 : : }
2184 : : break;
2185 : :
2186 : 14209569 : case TARGET_EXPR:
2187 : 14209569 : if (TARGET_EXPR_INITIAL (stmt)
2188 : 14209569 : && TREE_CODE (TARGET_EXPR_INITIAL (stmt)) == CONSTRUCTOR
2189 : 16672231 : && CONSTRUCTOR_PLACEHOLDER_BOUNDARY (TARGET_EXPR_INITIAL (stmt)))
2190 : 166 : TARGET_EXPR_NO_ELIDE (stmt) = 1;
2191 : : break;
2192 : :
2193 : 680 : case TEMPLATE_ID_EXPR:
2194 : 680 : gcc_assert (concept_check_p (stmt));
2195 : : /* Emit the value of the concept check. */
2196 : 680 : *stmt_p = evaluate_concept_check (stmt);
2197 : 680 : walk_subtrees = 0;
2198 : 680 : break;
2199 : :
2200 : 4223 : case OMP_DISTRIBUTE:
2201 : : /* Need to explicitly instantiate copy ctors on class iterators of
2202 : : composite distribute parallel for. */
2203 : 4223 : if (OMP_FOR_INIT (*stmt_p) == NULL_TREE)
2204 : : {
2205 : 3748 : tree *data[4] = { NULL, NULL, NULL, NULL };
2206 : 3748 : tree inner = walk_tree (&OMP_FOR_BODY (*stmt_p),
2207 : : find_combined_omp_for, data, NULL);
2208 : 3748 : if (inner != NULL_TREE
2209 : 3714 : && TREE_CODE (inner) == OMP_FOR)
2210 : : {
2211 : 4562 : for (int i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (inner)); i++)
2212 : 2861 : if (TREE_VEC_ELT (OMP_FOR_INIT (inner), i)
2213 : 2845 : && OMP_FOR_ORIG_DECLS (inner)
2214 : 2845 : && TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner),
2215 : : i)) == TREE_LIST
2216 : 2885 : && TREE_PURPOSE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner),
2217 : : i)))
2218 : : {
2219 : 12 : tree orig = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner), i);
2220 : : /* Class iterators aren't allowed on OMP_SIMD, so the only
2221 : : case we need to solve is distribute parallel for. */
2222 : 12 : gcc_assert (TREE_CODE (inner) == OMP_FOR
2223 : : && data[1]);
2224 : 12 : tree orig_decl = TREE_PURPOSE (orig);
2225 : 12 : tree c, cl = NULL_TREE;
2226 : 12 : for (c = OMP_FOR_CLAUSES (inner);
2227 : 16 : c; c = OMP_CLAUSE_CHAIN (c))
2228 : 12 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
2229 : 5 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
2230 : 13 : && OMP_CLAUSE_DECL (c) == orig_decl)
2231 : : {
2232 : : cl = c;
2233 : : break;
2234 : : }
2235 : 12 : if (cl == NULL_TREE)
2236 : : {
2237 : 4 : for (c = OMP_PARALLEL_CLAUSES (*data[1]);
2238 : 4 : c; c = OMP_CLAUSE_CHAIN (c))
2239 : 1 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
2240 : 1 : && OMP_CLAUSE_DECL (c) == orig_decl)
2241 : : {
2242 : : cl = c;
2243 : : break;
2244 : : }
2245 : : }
2246 : 4 : if (cl)
2247 : : {
2248 : 9 : orig_decl = require_complete_type (orig_decl);
2249 : 9 : tree inner_type = TREE_TYPE (orig_decl);
2250 : 9 : if (orig_decl == error_mark_node)
2251 : 0 : continue;
2252 : 9 : if (TYPE_REF_P (TREE_TYPE (orig_decl)))
2253 : 0 : inner_type = TREE_TYPE (inner_type);
2254 : :
2255 : 9 : while (TREE_CODE (inner_type) == ARRAY_TYPE)
2256 : 0 : inner_type = TREE_TYPE (inner_type);
2257 : 9 : get_copy_ctor (inner_type, tf_warning_or_error);
2258 : : }
2259 : : }
2260 : : }
2261 : : }
2262 : : /* FALLTHRU */
2263 : :
2264 : 53681254 : case FOR_STMT:
2265 : 53681254 : case WHILE_STMT:
2266 : 53681254 : case DO_STMT:
2267 : 53681254 : case SWITCH_STMT:
2268 : 53681254 : case CONTINUE_STMT:
2269 : 53681254 : case BREAK_STMT:
2270 : 53681254 : case OMP_FOR:
2271 : 53681254 : case OMP_SIMD:
2272 : 53681254 : case OMP_LOOP:
2273 : 53681254 : case OMP_TILE:
2274 : 53681254 : case OMP_UNROLL:
2275 : 53681254 : case OACC_LOOP:
2276 : 53681254 : case STATEMENT_LIST:
2277 : : /* These cases are handled by shared code. */
2278 : 53681254 : c_genericize_control_stmt (stmt_p, walk_subtrees, data,
2279 : : cp_genericize_r, cp_walk_subtrees);
2280 : 53681254 : break;
2281 : :
2282 : 5031 : case BIT_CAST_EXPR:
2283 : 5031 : *stmt_p = build1_loc (EXPR_LOCATION (stmt), VIEW_CONVERT_EXPR,
2284 : 5031 : TREE_TYPE (stmt), TREE_OPERAND (stmt, 0));
2285 : 5031 : break;
2286 : :
2287 : 24524885 : case MODIFY_EXPR:
2288 : : /* Mark stores to parts of complex automatic non-addressable
2289 : : variables as DECL_NOT_GIMPLE_REG_P for -O0. This can't be
2290 : : done during gimplification. See PR119120. */
2291 : 24524885 : if ((TREE_CODE (TREE_OPERAND (stmt, 0)) == REALPART_EXPR
2292 : 24495747 : || TREE_CODE (TREE_OPERAND (stmt, 0)) == IMAGPART_EXPR)
2293 : 58288 : && !optimize
2294 : 455 : && DECL_P (TREE_OPERAND (TREE_OPERAND (stmt, 0), 0))
2295 : 24524978 : && is_gimple_reg (TREE_OPERAND (TREE_OPERAND (stmt, 0), 0)))
2296 : 50 : DECL_NOT_GIMPLE_REG_P (TREE_OPERAND (TREE_OPERAND (stmt, 0), 0)) = 1;
2297 : : break;
2298 : :
2299 : 722152640 : default:
2300 : 722152640 : if (IS_TYPE_OR_DECL_P (stmt))
2301 : 217436183 : *walk_subtrees = 0;
2302 : : break;
2303 : : }
2304 : :
2305 : 1239632396 : p_set->add (*stmt_p);
2306 : :
2307 : 1239632396 : return NULL;
2308 : : }
2309 : :
2310 : : /* Lower C++ front end trees to GENERIC in T_P. */
2311 : :
2312 : : static void
2313 : 42090493 : cp_genericize_tree (tree* t_p, bool handle_invisiref_parm_p)
2314 : : {
2315 : 42090493 : struct cp_genericize_data wtd;
2316 : :
2317 : 42090493 : wtd.p_set = new hash_set<tree>;
2318 : 42090493 : wtd.bind_expr_stack.create (0);
2319 : 42090493 : wtd.omp_ctx = NULL;
2320 : 42090493 : wtd.try_block = NULL_TREE;
2321 : 42090493 : wtd.no_sanitize_p = false;
2322 : 42090493 : wtd.handle_invisiref_parm_p = handle_invisiref_parm_p;
2323 : 42090493 : cp_walk_tree (t_p, cp_genericize_r, &wtd, NULL);
2324 : 84180986 : delete wtd.p_set;
2325 : 42090493 : if (sanitize_flags_p (SANITIZE_VPTR))
2326 : 5228 : cp_ubsan_instrument_member_accesses (t_p);
2327 : 42090493 : }
2328 : :
2329 : : /* If a function that should end with a return in non-void
2330 : : function doesn't obviously end with return, add ubsan
2331 : : instrumentation code to verify it at runtime. If -fsanitize=return
2332 : : is not enabled, instrument __builtin_unreachable. */
2333 : :
2334 : : static void
2335 : 42090493 : cp_maybe_instrument_return (tree fndecl)
2336 : : {
2337 : 42090493 : if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fndecl)))
2338 : 60203124 : || DECL_CONSTRUCTOR_P (fndecl)
2339 : 30101562 : || DECL_DESTRUCTOR_P (fndecl)
2340 : 72192055 : || !targetm.warn_func_return (fndecl))
2341 : 11988943 : return;
2342 : :
2343 : 30101550 : if (!sanitize_flags_p (SANITIZE_RETURN, fndecl)
2344 : : /* Don't add __builtin_unreachable () if not optimizing, it will not
2345 : : improve any optimizations in that case, just break UB code.
2346 : : Don't add it if -fsanitize=unreachable -fno-sanitize=return either,
2347 : : UBSan covers this with ubsan_instrument_return above where sufficient
2348 : : information is provided, while the __builtin_unreachable () below
2349 : : if return sanitization is disabled will just result in hard to
2350 : : understand runtime error without location. */
2351 : 30101550 : && ((!optimize && !flag_unreachable_traps)
2352 : 30098063 : || sanitize_flags_p (SANITIZE_UNREACHABLE, fndecl)))
2353 : 30 : return;
2354 : :
2355 : 30101520 : tree t = DECL_SAVED_TREE (fndecl);
2356 : 54440111 : while (t)
2357 : : {
2358 : 54440111 : switch (TREE_CODE (t))
2359 : : {
2360 : 4159437 : case BIND_EXPR:
2361 : 4159437 : t = BIND_EXPR_BODY (t);
2362 : 4159437 : continue;
2363 : 8322390 : case TRY_FINALLY_EXPR:
2364 : 8322390 : case CLEANUP_POINT_EXPR:
2365 : 8322390 : t = TREE_OPERAND (t, 0);
2366 : 8322390 : continue;
2367 : 11859453 : case STATEMENT_LIST:
2368 : 11859453 : {
2369 : 11859453 : tree_stmt_iterator i = tsi_last (t);
2370 : 11864587 : while (!tsi_end_p (i))
2371 : : {
2372 : 11861898 : tree p = tsi_stmt (i);
2373 : 11861898 : if (TREE_CODE (p) != DEBUG_BEGIN_STMT)
2374 : : break;
2375 : 5134 : tsi_prev (&i);
2376 : : }
2377 : 11859453 : if (!tsi_end_p (i))
2378 : : {
2379 : 11856764 : t = tsi_stmt (i);
2380 : 11856764 : continue;
2381 : : }
2382 : : }
2383 : 2689 : break;
2384 : : case RETURN_EXPR:
2385 : : return;
2386 : : default:
2387 : : break;
2388 : 12481827 : }
2389 : : break;
2390 : : }
2391 : 14843307 : if (t == NULL_TREE)
2392 : : return;
2393 : 14843307 : tree *p = &DECL_SAVED_TREE (fndecl);
2394 : 14843307 : if (TREE_CODE (*p) == BIND_EXPR)
2395 : 960373 : p = &BIND_EXPR_BODY (*p);
2396 : :
2397 : 14843307 : location_t loc = DECL_SOURCE_LOCATION (fndecl);
2398 : 14843307 : if (sanitize_flags_p (SANITIZE_RETURN, fndecl))
2399 : 1383 : t = ubsan_instrument_return (loc);
2400 : : else
2401 : 14841924 : t = build_builtin_unreachable (BUILTINS_LOCATION);
2402 : :
2403 : 14843307 : append_to_statement_list (t, p);
2404 : : }
2405 : :
2406 : : void
2407 : 55881564 : cp_genericize (tree fndecl)
2408 : : {
2409 : 55881564 : tree t;
2410 : :
2411 : : /* Fix up the types of parms passed by invisible reference. */
2412 : 147098743 : for (t = DECL_ARGUMENTS (fndecl); t; t = DECL_CHAIN (t))
2413 : 91217179 : if (TREE_ADDRESSABLE (TREE_TYPE (t)))
2414 : : {
2415 : : /* If a function's arguments are copied to create a thunk,
2416 : : then DECL_BY_REFERENCE will be set -- but the type of the
2417 : : argument will be a pointer type, so we will never get
2418 : : here. */
2419 : 131573 : gcc_assert (!DECL_BY_REFERENCE (t));
2420 : 131573 : gcc_assert (DECL_ARG_TYPE (t) != TREE_TYPE (t));
2421 : 131573 : TREE_TYPE (t) = DECL_ARG_TYPE (t);
2422 : 131573 : DECL_BY_REFERENCE (t) = 1;
2423 : 131573 : TREE_ADDRESSABLE (t) = 0;
2424 : 131573 : relayout_decl (t);
2425 : : }
2426 : :
2427 : : /* Do the same for the return value. */
2428 : 55881564 : if (TREE_ADDRESSABLE (TREE_TYPE (DECL_RESULT (fndecl))))
2429 : : {
2430 : 1034822 : t = DECL_RESULT (fndecl);
2431 : 1034822 : TREE_TYPE (t) = build_reference_type (TREE_TYPE (t));
2432 : 1034822 : DECL_BY_REFERENCE (t) = 1;
2433 : 1034822 : TREE_ADDRESSABLE (t) = 0;
2434 : 1034822 : relayout_decl (t);
2435 : 1034822 : if (DECL_NAME (t))
2436 : : {
2437 : : /* Adjust DECL_VALUE_EXPR of the original var. */
2438 : 126527 : tree outer = outer_curly_brace_block (current_function_decl);
2439 : 126527 : tree var;
2440 : :
2441 : 126527 : if (outer)
2442 : 288918 : for (var = BLOCK_VARS (outer); var; var = DECL_CHAIN (var))
2443 : 288393 : if (VAR_P (var)
2444 : 278081 : && DECL_NAME (t) == DECL_NAME (var)
2445 : 126002 : && DECL_HAS_VALUE_EXPR_P (var)
2446 : 414395 : && DECL_VALUE_EXPR (var) == t)
2447 : : {
2448 : 126002 : tree val = convert_from_reference (t);
2449 : 126002 : SET_DECL_VALUE_EXPR (var, val);
2450 : 126002 : break;
2451 : : }
2452 : : }
2453 : : }
2454 : :
2455 : : /* If we're a clone, the body is already GIMPLE. */
2456 : 55881564 : if (DECL_CLONED_FUNCTION_P (fndecl))
2457 : 13791071 : return;
2458 : :
2459 : : /* Allow cp_genericize calls to be nested. */
2460 : 42090493 : bc_state_t save_state;
2461 : 42090493 : save_bc_state (&save_state);
2462 : :
2463 : : /* We do want to see every occurrence of the parms, so we can't just use
2464 : : walk_tree's hash functionality. */
2465 : 42090493 : cp_genericize_tree (&DECL_SAVED_TREE (fndecl), true);
2466 : :
2467 : 42090493 : cp_maybe_instrument_return (fndecl);
2468 : :
2469 : : /* Do everything else. */
2470 : 42090493 : c_genericize (fndecl);
2471 : 42090493 : restore_bc_state (&save_state);
2472 : : }
2473 : :
2474 : : /* Build code to apply FN to each member of ARG1 and ARG2. FN may be
2475 : : NULL if there is in fact nothing to do. ARG2 may be null if FN
2476 : : actually only takes one argument. */
2477 : :
2478 : : static tree
2479 : 3661 : cxx_omp_clause_apply_fn (tree fn, tree arg1, tree arg2)
2480 : : {
2481 : 3661 : tree defparm, parm, t;
2482 : 3661 : int i = 0;
2483 : 3661 : int nargs;
2484 : 3661 : tree *argarray;
2485 : :
2486 : 3661 : if (fn == NULL)
2487 : : return NULL;
2488 : :
2489 : 2827 : nargs = list_length (DECL_ARGUMENTS (fn));
2490 : 2827 : argarray = XALLOCAVEC (tree, nargs);
2491 : :
2492 : 2827 : defparm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
2493 : 2827 : if (arg2)
2494 : 944 : defparm = TREE_CHAIN (defparm);
2495 : :
2496 : 2827 : bool is_method = TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE;
2497 : 2827 : if (TREE_CODE (TREE_TYPE (arg1)) == ARRAY_TYPE)
2498 : : {
2499 : 27 : tree inner_type = TREE_TYPE (arg1);
2500 : 27 : tree start1, end1, p1;
2501 : 27 : tree start2 = NULL, p2 = NULL;
2502 : 27 : tree ret = NULL, lab;
2503 : :
2504 : 27 : start1 = arg1;
2505 : 27 : start2 = arg2;
2506 : 27 : do
2507 : : {
2508 : 27 : inner_type = TREE_TYPE (inner_type);
2509 : 27 : start1 = build4 (ARRAY_REF, inner_type, start1,
2510 : : size_zero_node, NULL, NULL);
2511 : 27 : if (arg2)
2512 : 9 : start2 = build4 (ARRAY_REF, inner_type, start2,
2513 : : size_zero_node, NULL, NULL);
2514 : : }
2515 : 27 : while (TREE_CODE (inner_type) == ARRAY_TYPE);
2516 : 27 : start1 = build_fold_addr_expr_loc (input_location, start1);
2517 : 27 : if (arg2)
2518 : 9 : start2 = build_fold_addr_expr_loc (input_location, start2);
2519 : :
2520 : 27 : end1 = TYPE_SIZE_UNIT (TREE_TYPE (arg1));
2521 : 27 : end1 = fold_build_pointer_plus (start1, end1);
2522 : :
2523 : 27 : p1 = create_tmp_var (TREE_TYPE (start1));
2524 : 27 : t = build2 (MODIFY_EXPR, TREE_TYPE (p1), p1, start1);
2525 : 27 : append_to_statement_list (t, &ret);
2526 : :
2527 : 27 : if (arg2)
2528 : : {
2529 : 9 : p2 = create_tmp_var (TREE_TYPE (start2));
2530 : 9 : t = build2 (MODIFY_EXPR, TREE_TYPE (p2), p2, start2);
2531 : 9 : append_to_statement_list (t, &ret);
2532 : : }
2533 : :
2534 : 27 : lab = create_artificial_label (input_location);
2535 : 27 : t = build1 (LABEL_EXPR, void_type_node, lab);
2536 : 27 : append_to_statement_list (t, &ret);
2537 : :
2538 : 27 : argarray[i++] = p1;
2539 : 27 : if (arg2)
2540 : 9 : argarray[i++] = p2;
2541 : : /* Handle default arguments. */
2542 : 27 : for (parm = defparm; parm && parm != void_list_node;
2543 : 0 : parm = TREE_CHAIN (parm), i++)
2544 : 0 : argarray[i] = convert_default_arg (TREE_VALUE (parm),
2545 : 0 : TREE_PURPOSE (parm), fn,
2546 : : i - is_method, tf_warning_or_error);
2547 : 27 : t = build_call_a (fn, i, argarray);
2548 : 27 : if (MAYBE_CLASS_TYPE_P (TREE_TYPE (t)))
2549 : 0 : t = build_cplus_new (TREE_TYPE (t), t, tf_warning_or_error);
2550 : 27 : t = fold_convert (void_type_node, t);
2551 : 27 : t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
2552 : 27 : append_to_statement_list (t, &ret);
2553 : :
2554 : 27 : t = fold_build_pointer_plus (p1, TYPE_SIZE_UNIT (inner_type));
2555 : 27 : t = build2 (MODIFY_EXPR, TREE_TYPE (p1), p1, t);
2556 : 27 : append_to_statement_list (t, &ret);
2557 : :
2558 : 27 : if (arg2)
2559 : : {
2560 : 9 : t = fold_build_pointer_plus (p2, TYPE_SIZE_UNIT (inner_type));
2561 : 9 : t = build2 (MODIFY_EXPR, TREE_TYPE (p2), p2, t);
2562 : 9 : append_to_statement_list (t, &ret);
2563 : : }
2564 : :
2565 : 27 : t = build2 (NE_EXPR, boolean_type_node, p1, end1);
2566 : 27 : t = build3 (COND_EXPR, void_type_node, t, build_and_jump (&lab), NULL);
2567 : 27 : append_to_statement_list (t, &ret);
2568 : :
2569 : 27 : return ret;
2570 : : }
2571 : : else
2572 : : {
2573 : 2800 : argarray[i++] = build_fold_addr_expr_loc (input_location, arg1);
2574 : 2800 : if (arg2)
2575 : 935 : argarray[i++] = build_fold_addr_expr_loc (input_location, arg2);
2576 : : /* Handle default arguments. */
2577 : 2805 : for (parm = defparm; parm && parm != void_list_node;
2578 : 5 : parm = TREE_CHAIN (parm), i++)
2579 : 10 : argarray[i] = convert_default_arg (TREE_VALUE (parm),
2580 : 5 : TREE_PURPOSE (parm), fn,
2581 : : i - is_method, tf_warning_or_error);
2582 : 2800 : t = build_call_a (fn, i, argarray);
2583 : 2800 : if (MAYBE_CLASS_TYPE_P (TREE_TYPE (t)))
2584 : 1 : t = build_cplus_new (TREE_TYPE (t), t, tf_warning_or_error);
2585 : 2800 : t = fold_convert (void_type_node, t);
2586 : 2800 : return fold_build_cleanup_point_expr (TREE_TYPE (t), t);
2587 : : }
2588 : : }
2589 : :
2590 : : /* Return code to initialize DECL with its default constructor, or
2591 : : NULL if there's nothing to do. */
2592 : :
2593 : : tree
2594 : 42521 : cxx_omp_clause_default_ctor (tree clause, tree decl, tree /*outer*/)
2595 : : {
2596 : 42521 : tree info = CP_OMP_CLAUSE_INFO (clause);
2597 : 42521 : tree ret = NULL;
2598 : :
2599 : 42521 : if (info)
2600 : 1389 : ret = cxx_omp_clause_apply_fn (TREE_VEC_ELT (info, 0), decl, NULL);
2601 : :
2602 : 42521 : return ret;
2603 : : }
2604 : :
2605 : : /* Return code to initialize DST with a copy constructor from SRC. */
2606 : :
2607 : : tree
2608 : 12393 : cxx_omp_clause_copy_ctor (tree clause, tree dst, tree src)
2609 : : {
2610 : 12393 : tree info = CP_OMP_CLAUSE_INFO (clause);
2611 : 12393 : tree ret = NULL;
2612 : :
2613 : 12393 : if (info)
2614 : 285 : ret = cxx_omp_clause_apply_fn (TREE_VEC_ELT (info, 0), dst, src);
2615 : 285 : if (ret == NULL)
2616 : 12175 : ret = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
2617 : :
2618 : 12393 : return ret;
2619 : : }
2620 : :
2621 : : /* Similarly, except use an assignment operator instead. */
2622 : :
2623 : : tree
2624 : 12679 : cxx_omp_clause_assign_op (tree clause, tree dst, tree src)
2625 : : {
2626 : 12679 : tree info = CP_OMP_CLAUSE_INFO (clause);
2627 : 12679 : tree ret = NULL;
2628 : :
2629 : 12679 : if (info)
2630 : 748 : ret = cxx_omp_clause_apply_fn (TREE_VEC_ELT (info, 2), dst, src);
2631 : 748 : if (ret == NULL)
2632 : 11953 : ret = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
2633 : :
2634 : 12679 : return ret;
2635 : : }
2636 : :
2637 : : /* Return code to destroy DECL. */
2638 : :
2639 : : tree
2640 : 62463 : cxx_omp_clause_dtor (tree clause, tree decl)
2641 : : {
2642 : 62463 : tree info = CP_OMP_CLAUSE_INFO (clause);
2643 : 62463 : tree ret = NULL;
2644 : :
2645 : 62463 : if (info)
2646 : 1239 : ret = cxx_omp_clause_apply_fn (TREE_VEC_ELT (info, 1), decl, NULL);
2647 : :
2648 : 62463 : return ret;
2649 : : }
2650 : :
2651 : : /* True if OpenMP should privatize what this DECL points to rather
2652 : : than the DECL itself. */
2653 : :
2654 : : bool
2655 : 934102 : cxx_omp_privatize_by_reference (const_tree decl)
2656 : : {
2657 : 934102 : return (TYPE_REF_P (TREE_TYPE (decl))
2658 : 934102 : || is_invisiref_parm (decl));
2659 : : }
2660 : :
2661 : : /* Return true if DECL is const qualified var having no mutable member. */
2662 : : bool
2663 : 15300 : cxx_omp_const_qual_no_mutable (tree decl)
2664 : : {
2665 : 15300 : tree type = TREE_TYPE (decl);
2666 : 15300 : if (TYPE_REF_P (type))
2667 : : {
2668 : 847 : if (!is_invisiref_parm (decl))
2669 : : return false;
2670 : 0 : type = TREE_TYPE (type);
2671 : :
2672 : 0 : if (TREE_CODE (decl) == RESULT_DECL && DECL_NAME (decl))
2673 : : {
2674 : : /* NVR doesn't preserve const qualification of the
2675 : : variable's type. */
2676 : 0 : tree outer = outer_curly_brace_block (current_function_decl);
2677 : 0 : tree var;
2678 : :
2679 : 0 : if (outer)
2680 : 0 : for (var = BLOCK_VARS (outer); var; var = DECL_CHAIN (var))
2681 : 0 : if (VAR_P (var)
2682 : 0 : && DECL_NAME (decl) == DECL_NAME (var)
2683 : 0 : && (TYPE_MAIN_VARIANT (type)
2684 : 0 : == TYPE_MAIN_VARIANT (TREE_TYPE (var))))
2685 : : {
2686 : 0 : if (TYPE_READONLY (TREE_TYPE (var)))
2687 : 0 : type = TREE_TYPE (var);
2688 : : break;
2689 : : }
2690 : : }
2691 : : }
2692 : :
2693 : 14453 : if (type == error_mark_node)
2694 : : return false;
2695 : :
2696 : : /* Variables with const-qualified type having no mutable member
2697 : : are predetermined shared. */
2698 : 14438 : if (TYPE_READONLY (type) && !cp_has_mutable_p (type))
2699 : : return true;
2700 : :
2701 : : return false;
2702 : : }
2703 : :
2704 : : /* OMP_CLAUSE_DEFAULT_UNSPECIFIED unless OpenMP sharing attribute
2705 : : of DECL is predetermined. */
2706 : :
2707 : : enum omp_clause_default_kind
2708 : 55382 : cxx_omp_predetermined_sharing_1 (tree decl)
2709 : : {
2710 : : /* Static data members are predetermined shared. */
2711 : 55382 : if (TREE_STATIC (decl))
2712 : : {
2713 : 15101 : tree ctx = CP_DECL_CONTEXT (decl);
2714 : 15101 : if (TYPE_P (ctx) && MAYBE_CLASS_TYPE_P (ctx))
2715 : : return OMP_CLAUSE_DEFAULT_SHARED;
2716 : :
2717 : 14995 : if (c_omp_predefined_variable (decl))
2718 : : return OMP_CLAUSE_DEFAULT_SHARED;
2719 : : }
2720 : :
2721 : : /* this may not be specified in data-sharing clauses, still we need
2722 : : to predetermined it firstprivate. */
2723 : 55231 : if (decl == current_class_ptr)
2724 : 110 : return OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
2725 : :
2726 : : return OMP_CLAUSE_DEFAULT_UNSPECIFIED;
2727 : : }
2728 : :
2729 : : /* Likewise, but also include the artificial vars. We don't want to
2730 : : disallow the artificial vars being mentioned in explicit clauses,
2731 : : as we use artificial vars e.g. for loop constructs with random
2732 : : access iterators other than pointers, but during gimplification
2733 : : we want to treat them as predetermined. */
2734 : :
2735 : : enum omp_clause_default_kind
2736 : 34924 : cxx_omp_predetermined_sharing (tree decl)
2737 : : {
2738 : 34924 : enum omp_clause_default_kind ret = cxx_omp_predetermined_sharing_1 (decl);
2739 : 34924 : if (ret != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
2740 : : return ret;
2741 : :
2742 : : /* Predetermine artificial variables holding integral values, those
2743 : : are usually result of gimplify_one_sizepos or SAVE_EXPR
2744 : : gimplification. */
2745 : 34710 : if (VAR_P (decl)
2746 : 22815 : && DECL_ARTIFICIAL (decl)
2747 : 6999 : && INTEGRAL_TYPE_P (TREE_TYPE (decl))
2748 : 35228 : && !(DECL_LANG_SPECIFIC (decl)
2749 : 2 : && DECL_OMP_PRIVATIZED_MEMBER (decl)))
2750 : : return OMP_CLAUSE_DEFAULT_SHARED;
2751 : :
2752 : : /* Similarly for typeinfo symbols. */
2753 : 34194 : if (VAR_P (decl) && DECL_ARTIFICIAL (decl) && DECL_TINFO_P (decl))
2754 : 57 : return OMP_CLAUSE_DEFAULT_SHARED;
2755 : :
2756 : : return OMP_CLAUSE_DEFAULT_UNSPECIFIED;
2757 : : }
2758 : :
2759 : : enum omp_clause_defaultmap_kind
2760 : 17956 : cxx_omp_predetermined_mapping (tree decl)
2761 : : {
2762 : : /* Predetermine artificial variables holding integral values, those
2763 : : are usually result of gimplify_one_sizepos or SAVE_EXPR
2764 : : gimplification. */
2765 : 17956 : if (VAR_P (decl)
2766 : 1679 : && DECL_ARTIFICIAL (decl)
2767 : 142 : && INTEGRAL_TYPE_P (TREE_TYPE (decl))
2768 : 18022 : && !(DECL_LANG_SPECIFIC (decl)
2769 : 6 : && DECL_OMP_PRIVATIZED_MEMBER (decl)))
2770 : : return OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
2771 : :
2772 : 17890 : if (c_omp_predefined_variable (decl))
2773 : 12 : return OMP_CLAUSE_DEFAULTMAP_TO;
2774 : :
2775 : : return OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
2776 : : }
2777 : :
2778 : : /* Finalize an implicitly determined clause. */
2779 : :
2780 : : void
2781 : 64198 : cxx_omp_finish_clause (tree c, gimple_seq *, bool /* openacc */)
2782 : : {
2783 : 64198 : tree decl, inner_type;
2784 : 64198 : bool make_shared = false;
2785 : :
2786 : 64198 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE
2787 : 56798 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_PRIVATE
2788 : 94292 : && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_LASTPRIVATE
2789 : 4753 : || !OMP_CLAUSE_LASTPRIVATE_LOOP_IV (c)))
2790 : : return;
2791 : :
2792 : 34119 : decl = OMP_CLAUSE_DECL (c);
2793 : 34119 : decl = require_complete_type (decl);
2794 : 34119 : inner_type = TREE_TYPE (decl);
2795 : 34119 : if (decl == error_mark_node)
2796 : 34119 : make_shared = true;
2797 : 34119 : else if (TYPE_REF_P (TREE_TYPE (decl)))
2798 : 89 : inner_type = TREE_TYPE (inner_type);
2799 : :
2800 : : /* We're interested in the base element, not arrays. */
2801 : 34373 : while (TREE_CODE (inner_type) == ARRAY_TYPE)
2802 : 254 : inner_type = TREE_TYPE (inner_type);
2803 : :
2804 : : /* Check for special function availability by building a call to one.
2805 : : Save the results, because later we won't be in the right context
2806 : : for making these queries. */
2807 : 34119 : bool first = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE;
2808 : 34119 : bool last = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE;
2809 : 34119 : if (!make_shared
2810 : 34119 : && CLASS_TYPE_P (inner_type)
2811 : 34345 : && cxx_omp_create_clause_info (c, inner_type, !first, first, last,
2812 : : true))
2813 : : make_shared = true;
2814 : :
2815 : 34113 : if (make_shared)
2816 : : {
2817 : 6 : OMP_CLAUSE_CODE (c) = OMP_CLAUSE_SHARED;
2818 : 6 : OMP_CLAUSE_SHARED_FIRSTPRIVATE (c) = 0;
2819 : 6 : OMP_CLAUSE_SHARED_READONLY (c) = 0;
2820 : : }
2821 : : }
2822 : :
2823 : : tree
2824 : 32 : cxx_omp_finish_mapper_clauses (tree clauses)
2825 : : {
2826 : 32 : return finish_omp_clauses (clauses, C_ORT_OMP);
2827 : : }
2828 : :
2829 : : /* Return true if DECL's DECL_VALUE_EXPR (if any) should be
2830 : : disregarded in OpenMP construct, because it is going to be
2831 : : remapped during OpenMP lowering. SHARED is true if DECL
2832 : : is going to be shared, false if it is going to be privatized. */
2833 : :
2834 : : bool
2835 : 670602 : cxx_omp_disregard_value_expr (tree decl, bool shared)
2836 : : {
2837 : 670602 : if (shared)
2838 : : return false;
2839 : 404131 : if (VAR_P (decl)
2840 : 382058 : && DECL_HAS_VALUE_EXPR_P (decl)
2841 : 9396 : && DECL_ARTIFICIAL (decl)
2842 : 8896 : && DECL_LANG_SPECIFIC (decl)
2843 : 411934 : && DECL_OMP_PRIVATIZED_MEMBER (decl))
2844 : : return true;
2845 : 398793 : if (VAR_P (decl) && DECL_CONTEXT (decl) && is_capture_proxy (decl))
2846 : : return true;
2847 : : return false;
2848 : : }
2849 : :
2850 : : /* Fold expression X which is used as an rvalue if RVAL is true. */
2851 : :
2852 : : static tree
2853 : 1510327569 : cp_fold_maybe_rvalue (tree x, bool rval, fold_flags_t flags)
2854 : : {
2855 : 1537822027 : while (true)
2856 : : {
2857 : 1524074798 : x = cp_fold (x, flags);
2858 : 1524074798 : if (rval)
2859 : 1092945016 : x = mark_rvalue_use (x);
2860 : 1092945016 : if (rval && DECL_P (x)
2861 : 277368824 : && !TYPE_REF_P (TREE_TYPE (x)))
2862 : : {
2863 : 240489999 : tree v = decl_constant_value (x);
2864 : 240489999 : if (v != x && v != error_mark_node)
2865 : : {
2866 : 13747229 : x = v;
2867 : 13747229 : continue;
2868 : : }
2869 : : }
2870 : 1510327569 : break;
2871 : 13747229 : }
2872 : 1510327569 : return x;
2873 : : }
2874 : :
2875 : : tree
2876 : 62249211 : cp_fold_maybe_rvalue (tree x, bool rval)
2877 : : {
2878 : 62249211 : return cp_fold_maybe_rvalue (x, rval, ff_none);
2879 : : }
2880 : :
2881 : : /* Fold expression X which is used as an rvalue. */
2882 : :
2883 : : static tree
2884 : 506239602 : cp_fold_rvalue (tree x, fold_flags_t flags)
2885 : : {
2886 : 315674130 : return cp_fold_maybe_rvalue (x, true, flags);
2887 : : }
2888 : :
2889 : : tree
2890 : 316792 : cp_fold_rvalue (tree x)
2891 : : {
2892 : 316792 : return cp_fold_rvalue (x, ff_none);
2893 : : }
2894 : :
2895 : : /* Perform folding on expression X. */
2896 : :
2897 : : static tree
2898 : 208274479 : cp_fully_fold (tree x, mce_value manifestly_const_eval)
2899 : : {
2900 : 208274479 : if (processing_template_decl)
2901 : : return x;
2902 : : /* FIXME cp_fold ought to be a superset of maybe_constant_value so we don't
2903 : : have to call both. */
2904 : 190248680 : if (cxx_dialect >= cxx11)
2905 : : {
2906 : 189228464 : x = maybe_constant_value (x, /*decl=*/NULL_TREE, manifestly_const_eval);
2907 : : /* Sometimes we are given a CONSTRUCTOR but the call above wraps it into
2908 : : a TARGET_EXPR; undo that here. */
2909 : 189228464 : if (TREE_CODE (x) == TARGET_EXPR)
2910 : 706814 : x = TARGET_EXPR_INITIAL (x);
2911 : 188521650 : else if (TREE_CODE (x) == VIEW_CONVERT_EXPR
2912 : 25797268 : && TREE_CODE (TREE_OPERAND (x, 0)) == CONSTRUCTOR
2913 : 188521839 : && TREE_TYPE (TREE_OPERAND (x, 0)) == TREE_TYPE (x))
2914 : 189 : x = TREE_OPERAND (x, 0);
2915 : : }
2916 : 190248680 : fold_flags_t flags = ff_none;
2917 : 190248680 : if (manifestly_const_eval == mce_false)
2918 : 32205158 : flags |= ff_mce_false;
2919 : 190248680 : return cp_fold_rvalue (x, flags);
2920 : : }
2921 : :
2922 : : tree
2923 : 176069321 : cp_fully_fold (tree x)
2924 : : {
2925 : 176069321 : return cp_fully_fold (x, mce_unknown);
2926 : : }
2927 : :
2928 : : /* Likewise, but also fold recursively, which cp_fully_fold doesn't perform
2929 : : in some cases. */
2930 : :
2931 : : tree
2932 : 33831973 : cp_fully_fold_init (tree x)
2933 : : {
2934 : 33831973 : if (processing_template_decl)
2935 : 1626815 : return x;
2936 : 32205158 : x = cp_fully_fold (x, mce_false);
2937 : 32205158 : cp_fold_data data (ff_mce_false);
2938 : 32205158 : if (cxx_dialect >= cxx20)
2939 : : {
2940 : 13214163 : cp_walk_tree (&x, cp_fold_immediate_r, &data, NULL);
2941 : 13214163 : data.pset.empty ();
2942 : : }
2943 : 32205158 : cp_walk_tree (&x, cp_fold_r, &data, NULL);
2944 : 32205158 : return x;
2945 : 32205158 : }
2946 : :
2947 : : /* c-common interface to cp_fold. If IN_INIT, this is in a static initializer
2948 : : and certain changes are made to the folding done. Or should be (FIXME). We
2949 : : never touch maybe_const, as it is only used for the C front-end
2950 : : C_MAYBE_CONST_EXPR. */
2951 : :
2952 : : tree
2953 : 62249211 : c_fully_fold (tree x, bool /*in_init*/, bool */*maybe_const*/, bool lval)
2954 : : {
2955 : 62249211 : return cp_fold_maybe_rvalue (x, !lval);
2956 : : }
2957 : :
2958 : : static GTY((deletable)) hash_map<tree, tree> *fold_caches[2];
2959 : :
2960 : : /* Subroutine of cp_fold. Returns which fold cache to use according
2961 : : to the given flags. We need multiple caches since the result of
2962 : : folding may depend on which flags are used. */
2963 : :
2964 : : static hash_map<tree, tree> *&
2965 : 2242095277 : get_fold_cache (fold_flags_t flags)
2966 : : {
2967 : 0 : if (flags & ff_mce_false)
2968 : 1921996994 : return fold_caches[1];
2969 : : else
2970 : 320098283 : return fold_caches[0];
2971 : : }
2972 : :
2973 : : /* Dispose of the whole FOLD_CACHE. */
2974 : :
2975 : : void
2976 : 25099401 : clear_fold_cache (void)
2977 : : {
2978 : 75298203 : for (auto& fold_cache : fold_caches)
2979 : 50198802 : if (fold_cache != NULL)
2980 : 61615030 : fold_cache->empty ();
2981 : 25099401 : }
2982 : :
2983 : : /* This function tries to fold an expression X.
2984 : : To avoid combinatorial explosion, folding results are kept in fold_cache.
2985 : : If X is invalid, we don't fold at all.
2986 : : For performance reasons we don't cache expressions representing a
2987 : : declaration or constant.
2988 : : Function returns X or its folded variant. */
2989 : :
2990 : : static tree
2991 : 3663033867 : cp_fold (tree x, fold_flags_t flags)
2992 : : {
2993 : 3663033867 : tree op0, op1, op2, op3;
2994 : 3663033867 : tree org_x = x, r = NULL_TREE;
2995 : 3663033867 : enum tree_code code;
2996 : 3663033867 : location_t loc;
2997 : 3663033867 : bool rval_ops = true;
2998 : :
2999 : 3663033867 : if (!x || x == error_mark_node)
3000 : : return x;
3001 : :
3002 : 3659606628 : if (EXPR_P (x) && (!TREE_TYPE (x) || TREE_TYPE (x) == error_mark_node))
3003 : : return x;
3004 : :
3005 : : /* Don't bother to cache DECLs or constants. */
3006 : 3659474092 : if (DECL_P (x) || CONSTANT_CLASS_P (x))
3007 : : return x;
3008 : :
3009 : 2242095277 : auto& fold_cache = get_fold_cache (flags);
3010 : 2242095277 : if (fold_cache == NULL)
3011 : 310928 : fold_cache = hash_map<tree, tree>::create_ggc (101);
3012 : :
3013 : 2242095277 : if (tree *cached = fold_cache->get (x))
3014 : : {
3015 : : /* unshare_expr doesn't recurse into SAVE_EXPRs. If SAVE_EXPR's
3016 : : argument has been folded into a tree invariant, make sure it is
3017 : : unshared. See PR112727. */
3018 : 451390309 : if (TREE_CODE (x) == SAVE_EXPR && *cached != x)
3019 : 86 : return unshare_expr (*cached);
3020 : 451390223 : return *cached;
3021 : : }
3022 : :
3023 : 1790704968 : uid_sensitive_constexpr_evaluation_checker c;
3024 : :
3025 : 1790704968 : code = TREE_CODE (x);
3026 : 1790704968 : switch (code)
3027 : : {
3028 : 97171480 : case CLEANUP_POINT_EXPR:
3029 : : /* Strip CLEANUP_POINT_EXPR if the expression doesn't have side
3030 : : effects. */
3031 : 97171480 : r = cp_fold_rvalue (TREE_OPERAND (x, 0), flags);
3032 : 97171480 : if (!TREE_SIDE_EFFECTS (r))
3033 : 1389507 : x = r;
3034 : : break;
3035 : :
3036 : 1402530 : case SIZEOF_EXPR:
3037 : 1402530 : x = fold_sizeof_expr (x);
3038 : 1402530 : break;
3039 : :
3040 : 204285980 : case VIEW_CONVERT_EXPR:
3041 : 204285980 : rval_ops = false;
3042 : : /* FALLTHRU */
3043 : 545905343 : case NON_LVALUE_EXPR:
3044 : 545905343 : CASE_CONVERT:
3045 : :
3046 : 545905343 : if (VOID_TYPE_P (TREE_TYPE (x)))
3047 : : {
3048 : : /* This is just to make sure we don't end up with casts to
3049 : : void from error_mark_node. If we just return x, then
3050 : : cp_fold_r might fold the operand into error_mark_node and
3051 : : leave the conversion in the IR. STRIP_USELESS_TYPE_CONVERSION
3052 : : during gimplification doesn't like such casts.
3053 : : Don't create a new tree if op0 != TREE_OPERAND (x, 0), the
3054 : : folding of the operand should be in the caches and if in cp_fold_r
3055 : : it will modify it in place. */
3056 : 39673004 : op0 = cp_fold (TREE_OPERAND (x, 0), flags);
3057 : 39673004 : if (op0 == error_mark_node)
3058 : 92 : x = error_mark_node;
3059 : : break;
3060 : : }
3061 : :
3062 : 506232339 : loc = EXPR_LOCATION (x);
3063 : 506232339 : op0 = cp_fold_maybe_rvalue (TREE_OPERAND (x, 0), rval_ops, flags);
3064 : :
3065 : 506232339 : if (op0 == error_mark_node)
3066 : 0 : x = error_mark_node;
3067 : 506232339 : else if (code == CONVERT_EXPR
3068 : 37088689 : && SCALAR_TYPE_P (TREE_TYPE (x))
3069 : 543319738 : && op0 != void_node)
3070 : : /* During parsing we used convert_to_*_nofold; re-convert now using the
3071 : : folding variants, since fold() doesn't do those transformations. */
3072 : 33752856 : x = fold (convert (TREE_TYPE (x), op0));
3073 : 472479483 : else if (op0 != TREE_OPERAND (x, 0))
3074 : 125151446 : x = fold_build1_loc (loc, code, TREE_TYPE (x), op0);
3075 : : else
3076 : 347328037 : x = fold (x);
3077 : :
3078 : : /* Conversion of an out-of-range value has implementation-defined
3079 : : behavior; the language considers it different from arithmetic
3080 : : overflow, which is undefined. */
3081 : 506232339 : if (TREE_CODE (op0) == INTEGER_CST
3082 : 506232339 : && TREE_OVERFLOW_P (x) && !TREE_OVERFLOW_P (op0))
3083 : 44 : TREE_OVERFLOW (x) = false;
3084 : :
3085 : : break;
3086 : :
3087 : 180 : case EXCESS_PRECISION_EXPR:
3088 : 180 : op0 = cp_fold_maybe_rvalue (TREE_OPERAND (x, 0), rval_ops, flags);
3089 : 180 : x = fold_convert_loc (EXPR_LOCATION (x), TREE_TYPE (x), op0);
3090 : 180 : break;
3091 : :
3092 : 60154947 : case INDIRECT_REF:
3093 : : /* We don't need the decltype(auto) obfuscation anymore. */
3094 : 60154947 : if (REF_PARENTHESIZED_P (x))
3095 : : {
3096 : 577 : tree p = maybe_undo_parenthesized_ref (x);
3097 : 577 : if (p != x)
3098 : 0 : return cp_fold (p, flags);
3099 : : }
3100 : 60154947 : goto unary;
3101 : :
3102 : 133206614 : case ADDR_EXPR:
3103 : 133206614 : loc = EXPR_LOCATION (x);
3104 : 133206614 : op0 = cp_fold_maybe_rvalue (TREE_OPERAND (x, 0), false, flags);
3105 : :
3106 : : /* Cope with user tricks that amount to offsetof. */
3107 : 133206614 : if (op0 != error_mark_node
3108 : 133206614 : && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (op0)))
3109 : : {
3110 : 52396159 : tree val = get_base_address (op0);
3111 : 52396159 : if (val
3112 : 52396159 : && INDIRECT_REF_P (val)
3113 : 21112050 : && COMPLETE_TYPE_P (TREE_TYPE (val))
3114 : 73508119 : && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3115 : : {
3116 : 250 : val = TREE_OPERAND (val, 0);
3117 : 250 : STRIP_NOPS (val);
3118 : 250 : val = maybe_constant_value (val);
3119 : 250 : if (TREE_CODE (val) == INTEGER_CST)
3120 : 127 : return fold_offsetof (op0, TREE_TYPE (x));
3121 : : }
3122 : : }
3123 : 133206487 : goto finish_unary;
3124 : :
3125 : : case REALPART_EXPR:
3126 : : case IMAGPART_EXPR:
3127 : 89053106 : rval_ops = false;
3128 : : /* FALLTHRU */
3129 : 89053106 : case CONJ_EXPR:
3130 : 89053106 : case FIX_TRUNC_EXPR:
3131 : 89053106 : case FLOAT_EXPR:
3132 : 89053106 : case NEGATE_EXPR:
3133 : 89053106 : case ABS_EXPR:
3134 : 89053106 : case ABSU_EXPR:
3135 : 89053106 : case BIT_NOT_EXPR:
3136 : 89053106 : case TRUTH_NOT_EXPR:
3137 : 89053106 : case FIXED_CONVERT_EXPR:
3138 : 89053106 : unary:
3139 : :
3140 : 89053106 : loc = EXPR_LOCATION (x);
3141 : 89053106 : op0 = cp_fold_maybe_rvalue (TREE_OPERAND (x, 0), rval_ops, flags);
3142 : :
3143 : 222259593 : finish_unary:
3144 : 222259593 : if (op0 == error_mark_node)
3145 : 0 : x = error_mark_node;
3146 : 222259593 : else if (op0 != TREE_OPERAND (x, 0))
3147 : : {
3148 : 29521777 : x = fold_build1_loc (loc, code, TREE_TYPE (x), op0);
3149 : 29521777 : if (code == INDIRECT_REF
3150 : 7564239 : && (INDIRECT_REF_P (x) || TREE_CODE (x) == MEM_REF))
3151 : : {
3152 : 7564112 : TREE_READONLY (x) = TREE_READONLY (org_x);
3153 : 7564112 : TREE_SIDE_EFFECTS (x) = TREE_SIDE_EFFECTS (org_x);
3154 : 7564112 : TREE_THIS_VOLATILE (x) = TREE_THIS_VOLATILE (org_x);
3155 : : }
3156 : : }
3157 : : else
3158 : 192737816 : x = fold (x);
3159 : :
3160 : 222259593 : gcc_assert (TREE_CODE (x) != COND_EXPR
3161 : : || !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))));
3162 : : break;
3163 : :
3164 : 197217 : case UNARY_PLUS_EXPR:
3165 : 197217 : op0 = cp_fold_rvalue (TREE_OPERAND (x, 0), flags);
3166 : 197217 : if (op0 == error_mark_node)
3167 : 0 : x = error_mark_node;
3168 : : else
3169 : 197217 : x = fold_convert (TREE_TYPE (x), op0);
3170 : : break;
3171 : :
3172 : 91686075 : case POSTDECREMENT_EXPR:
3173 : 91686075 : case POSTINCREMENT_EXPR:
3174 : 91686075 : case INIT_EXPR:
3175 : 91686075 : case PREDECREMENT_EXPR:
3176 : 91686075 : case PREINCREMENT_EXPR:
3177 : 91686075 : case COMPOUND_EXPR:
3178 : 91686075 : case MODIFY_EXPR:
3179 : 91686075 : rval_ops = false;
3180 : : /* FALLTHRU */
3181 : 213346517 : case POINTER_PLUS_EXPR:
3182 : 213346517 : case PLUS_EXPR:
3183 : 213346517 : case POINTER_DIFF_EXPR:
3184 : 213346517 : case MINUS_EXPR:
3185 : 213346517 : case MULT_EXPR:
3186 : 213346517 : case TRUNC_DIV_EXPR:
3187 : 213346517 : case CEIL_DIV_EXPR:
3188 : 213346517 : case FLOOR_DIV_EXPR:
3189 : 213346517 : case ROUND_DIV_EXPR:
3190 : 213346517 : case TRUNC_MOD_EXPR:
3191 : 213346517 : case CEIL_MOD_EXPR:
3192 : 213346517 : case ROUND_MOD_EXPR:
3193 : 213346517 : case RDIV_EXPR:
3194 : 213346517 : case EXACT_DIV_EXPR:
3195 : 213346517 : case MIN_EXPR:
3196 : 213346517 : case MAX_EXPR:
3197 : 213346517 : case LSHIFT_EXPR:
3198 : 213346517 : case RSHIFT_EXPR:
3199 : 213346517 : case LROTATE_EXPR:
3200 : 213346517 : case RROTATE_EXPR:
3201 : 213346517 : case BIT_AND_EXPR:
3202 : 213346517 : case BIT_IOR_EXPR:
3203 : 213346517 : case BIT_XOR_EXPR:
3204 : 213346517 : case TRUTH_AND_EXPR:
3205 : 213346517 : case TRUTH_ANDIF_EXPR:
3206 : 213346517 : case TRUTH_OR_EXPR:
3207 : 213346517 : case TRUTH_ORIF_EXPR:
3208 : 213346517 : case TRUTH_XOR_EXPR:
3209 : 213346517 : case LT_EXPR: case LE_EXPR:
3210 : 213346517 : case GT_EXPR: case GE_EXPR:
3211 : 213346517 : case EQ_EXPR: case NE_EXPR:
3212 : 213346517 : case UNORDERED_EXPR: case ORDERED_EXPR:
3213 : 213346517 : case UNLT_EXPR: case UNLE_EXPR:
3214 : 213346517 : case UNGT_EXPR: case UNGE_EXPR:
3215 : 213346517 : case UNEQ_EXPR: case LTGT_EXPR:
3216 : 213346517 : case RANGE_EXPR: case COMPLEX_EXPR:
3217 : :
3218 : 213346517 : loc = EXPR_LOCATION (x);
3219 : 213346517 : op0 = cp_fold_maybe_rvalue (TREE_OPERAND (x, 0), rval_ops, flags);
3220 : 213346517 : op1 = cp_fold_rvalue (TREE_OPERAND (x, 1), flags);
3221 : :
3222 : : /* decltype(nullptr) has only one value, so optimize away all comparisons
3223 : : with that type right away, keeping them in the IL causes troubles for
3224 : : various optimizations. */
3225 : 213346517 : if (COMPARISON_CLASS_P (org_x)
3226 : 33122349 : && TREE_CODE (TREE_TYPE (op0)) == NULLPTR_TYPE
3227 : 213346547 : && TREE_CODE (TREE_TYPE (op1)) == NULLPTR_TYPE)
3228 : : {
3229 : 30 : switch (code)
3230 : : {
3231 : 15 : case EQ_EXPR:
3232 : 15 : x = constant_boolean_node (true, TREE_TYPE (x));
3233 : 15 : break;
3234 : 15 : case NE_EXPR:
3235 : 15 : x = constant_boolean_node (false, TREE_TYPE (x));
3236 : 15 : break;
3237 : 0 : default:
3238 : 0 : gcc_unreachable ();
3239 : : }
3240 : 30 : return omit_two_operands_loc (loc, TREE_TYPE (x), x,
3241 : 30 : op0, op1);
3242 : : }
3243 : :
3244 : 213346487 : if (op0 == error_mark_node || op1 == error_mark_node)
3245 : 108 : x = error_mark_node;
3246 : 213346379 : else if (op0 != TREE_OPERAND (x, 0) || op1 != TREE_OPERAND (x, 1))
3247 : 145911163 : x = fold_build2_loc (loc, code, TREE_TYPE (x), op0, op1);
3248 : : else
3249 : 67435216 : x = fold (x);
3250 : :
3251 : : /* This is only needed for -Wnonnull-compare and only if
3252 : : TREE_NO_WARNING (org_x), but to avoid that option affecting code
3253 : : generation, we do it always. */
3254 : 213346487 : if (COMPARISON_CLASS_P (org_x))
3255 : : {
3256 : 33122319 : if (x == error_mark_node || TREE_CODE (x) == INTEGER_CST)
3257 : : ;
3258 : 31966195 : else if (COMPARISON_CLASS_P (x))
3259 : : {
3260 : 31053333 : if (warn_nonnull_compare
3261 : 31053333 : && warning_suppressed_p (org_x, OPT_Wnonnull_compare))
3262 : 116529 : suppress_warning (x, OPT_Wnonnull_compare);
3263 : : }
3264 : : /* Otherwise give up on optimizing these, let GIMPLE folders
3265 : : optimize those later on. */
3266 : 912862 : else if (op0 != TREE_OPERAND (org_x, 0)
3267 : 912862 : || op1 != TREE_OPERAND (org_x, 1))
3268 : : {
3269 : 912344 : x = build2_loc (loc, code, TREE_TYPE (org_x), op0, op1);
3270 : 912344 : if (warn_nonnull_compare
3271 : 912344 : && warning_suppressed_p (org_x, OPT_Wnonnull_compare))
3272 : 16 : suppress_warning (x, OPT_Wnonnull_compare);
3273 : : }
3274 : : else
3275 : 518 : x = org_x;
3276 : : }
3277 : :
3278 : : break;
3279 : :
3280 : 4958916 : case VEC_COND_EXPR:
3281 : 4958916 : case COND_EXPR:
3282 : 4958916 : loc = EXPR_LOCATION (x);
3283 : 4958916 : op0 = cp_fold_rvalue (TREE_OPERAND (x, 0), flags);
3284 : 4958916 : op1 = cp_fold (TREE_OPERAND (x, 1), flags);
3285 : 4958916 : op2 = cp_fold (TREE_OPERAND (x, 2), flags);
3286 : :
3287 : 4958916 : if (TREE_CODE (TREE_TYPE (x)) == BOOLEAN_TYPE)
3288 : : {
3289 : 20559 : warning_sentinel s (warn_int_in_bool_context);
3290 : 20559 : if (!VOID_TYPE_P (TREE_TYPE (op1)))
3291 : 20559 : op1 = cp_truthvalue_conversion (op1, tf_warning_or_error);
3292 : 20559 : if (!VOID_TYPE_P (TREE_TYPE (op2)))
3293 : 20538 : op2 = cp_truthvalue_conversion (op2, tf_warning_or_error);
3294 : 20559 : }
3295 : 4938357 : else if (VOID_TYPE_P (TREE_TYPE (x)))
3296 : : {
3297 : 1539660 : if (TREE_CODE (op0) == INTEGER_CST)
3298 : : {
3299 : : /* If the condition is constant, fold can fold away
3300 : : the COND_EXPR. If some statement-level uses of COND_EXPR
3301 : : have one of the branches NULL, avoid folding crash. */
3302 : 281615 : if (!op1)
3303 : 0 : op1 = build_empty_stmt (loc);
3304 : 281615 : if (!op2)
3305 : 12 : op2 = build_empty_stmt (loc);
3306 : : }
3307 : : else
3308 : : {
3309 : : /* Otherwise, don't bother folding a void condition, since
3310 : : it can't produce a constant value. */
3311 : 1258045 : if (op0 != TREE_OPERAND (x, 0)
3312 : 1178876 : || op1 != TREE_OPERAND (x, 1)
3313 : 2215653 : || op2 != TREE_OPERAND (x, 2))
3314 : 302642 : x = build3_loc (loc, code, TREE_TYPE (x), op0, op1, op2);
3315 : : break;
3316 : : }
3317 : : }
3318 : :
3319 : 3700871 : if (op0 == error_mark_node
3320 : 3700871 : || op1 == error_mark_node
3321 : 3700857 : || op2 == error_mark_node)
3322 : 62 : x = error_mark_node;
3323 : 3700809 : else if (op0 != TREE_OPERAND (x, 0)
3324 : 1439418 : || op1 != TREE_OPERAND (x, 1)
3325 : 4823312 : || op2 != TREE_OPERAND (x, 2))
3326 : 2648442 : x = fold_build3_loc (loc, code, TREE_TYPE (x), op0, op1, op2);
3327 : : else
3328 : 1052367 : x = fold (x);
3329 : :
3330 : : /* A COND_EXPR might have incompatible types in branches if one or both
3331 : : arms are bitfields. If folding exposed such a branch, fix it up. */
3332 : 3700871 : if (TREE_CODE (x) != code
3333 : 858328 : && x != error_mark_node
3334 : 4559137 : && !useless_type_conversion_p (TREE_TYPE (org_x), TREE_TYPE (x)))
3335 : 18591 : x = fold_convert (TREE_TYPE (org_x), x);
3336 : :
3337 : : break;
3338 : :
3339 : 114198149 : case CALL_EXPR:
3340 : 114198149 : {
3341 : 114198149 : tree callee = get_callee_fndecl (x);
3342 : :
3343 : : /* "Inline" calls to std::move/forward and other cast-like functions
3344 : : by simply folding them into a corresponding cast to their return
3345 : : type. This is cheaper than relying on the middle end to do so, and
3346 : : also means we avoid generating useless debug info for them at all.
3347 : :
3348 : : At this point the argument has already been converted into a
3349 : : reference, so it suffices to use a NOP_EXPR to express the
3350 : : cast. */
3351 : 114198149 : if ((OPTION_SET_P (flag_fold_simple_inlines)
3352 : 114198149 : ? flag_fold_simple_inlines
3353 : 114198031 : : !flag_no_inline)
3354 : 110819937 : && call_expr_nargs (x) == 1
3355 : 59148450 : && decl_in_std_namespace_p (callee)
3356 : 38601725 : && DECL_NAME (callee) != NULL_TREE
3357 : 152799874 : && (id_equal (DECL_NAME (callee), "move")
3358 : 37428971 : || id_equal (DECL_NAME (callee), "forward")
3359 : 36196002 : || id_equal (DECL_NAME (callee), "forward_like")
3360 : 36195972 : || id_equal (DECL_NAME (callee), "addressof")
3361 : : /* This addressof equivalent is used heavily in libstdc++. */
3362 : 35986868 : || id_equal (DECL_NAME (callee), "__addressof")
3363 : 35724580 : || id_equal (DECL_NAME (callee), "to_underlying")
3364 : 35724576 : || id_equal (DECL_NAME (callee), "as_const")))
3365 : : {
3366 : 2877833 : r = CALL_EXPR_ARG (x, 0);
3367 : 2877833 : r = build_nop (TREE_TYPE (x), r);
3368 : 2877833 : x = cp_fold (r, flags);
3369 : 2877833 : break;
3370 : : }
3371 : :
3372 : 111320316 : int sv = optimize, nw = sv;
3373 : :
3374 : : /* Some built-in function calls will be evaluated at compile-time in
3375 : : fold (). Set optimize to 1 when folding __builtin_constant_p inside
3376 : : a constexpr function so that fold_builtin_1 doesn't fold it to 0. */
3377 : 109929215 : if (callee && fndecl_built_in_p (callee) && !optimize
3378 : 659306 : && DECL_IS_BUILTIN_CONSTANT_P (callee)
3379 : 11885 : && current_function_decl
3380 : 111332185 : && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
3381 : : nw = 1;
3382 : :
3383 : 111320316 : if (callee && fndecl_built_in_p (callee, BUILT_IN_FRONTEND))
3384 : : {
3385 : 34298 : iloc_sentinel ils (EXPR_LOCATION (x));
3386 : 34298 : switch (DECL_FE_FUNCTION_CODE (callee))
3387 : : {
3388 : 33444 : case CP_BUILT_IN_IS_CONSTANT_EVALUATED:
3389 : : /* Defer folding __builtin_is_constant_evaluated unless
3390 : : we know this isn't a manifestly constant-evaluated
3391 : : context. */
3392 : 33444 : if (flags & ff_mce_false)
3393 : 17101 : x = boolean_false_node;
3394 : : break;
3395 : 0 : case CP_BUILT_IN_SOURCE_LOCATION:
3396 : 0 : x = fold_builtin_source_location (x);
3397 : 0 : break;
3398 : 444 : case CP_BUILT_IN_IS_CORRESPONDING_MEMBER:
3399 : 888 : x = fold_builtin_is_corresponding_member
3400 : 444 : (EXPR_LOCATION (x), call_expr_nargs (x),
3401 : : &CALL_EXPR_ARG (x, 0));
3402 : 444 : break;
3403 : 410 : case CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS:
3404 : 820 : x = fold_builtin_is_pointer_inverconvertible_with_class
3405 : 410 : (EXPR_LOCATION (x), call_expr_nargs (x),
3406 : : &CALL_EXPR_ARG (x, 0));
3407 : 410 : break;
3408 : : default:
3409 : : break;
3410 : : }
3411 : 34298 : break;
3412 : 34298 : }
3413 : :
3414 : 111286018 : if (callee
3415 : 111286018 : && fndecl_built_in_p (callee, CP_BUILT_IN_SOURCE_LOCATION,
3416 : : BUILT_IN_FRONTEND))
3417 : : {
3418 : 0 : x = fold_builtin_source_location (x);
3419 : 0 : break;
3420 : : }
3421 : :
3422 : 111286018 : bool changed = false;
3423 : 111286018 : int m = call_expr_nargs (x);
3424 : 275140823 : for (int i = 0; i < m; i++)
3425 : : {
3426 : 163854805 : r = cp_fold (CALL_EXPR_ARG (x, i), flags);
3427 : 163854805 : if (r != CALL_EXPR_ARG (x, i))
3428 : : {
3429 : 94219121 : if (r == error_mark_node)
3430 : : {
3431 : 0 : x = error_mark_node;
3432 : 0 : break;
3433 : : }
3434 : 94219121 : if (!changed)
3435 : 60074088 : x = copy_node (x);
3436 : 94219121 : CALL_EXPR_ARG (x, i) = r;
3437 : 94219121 : changed = true;
3438 : : }
3439 : : }
3440 : 111286018 : if (x == error_mark_node)
3441 : : break;
3442 : :
3443 : 111286018 : optimize = nw;
3444 : 111286018 : r = fold (x);
3445 : 111286018 : optimize = sv;
3446 : :
3447 : 111286018 : if (TREE_CODE (r) != CALL_EXPR)
3448 : : {
3449 : 2576516 : x = cp_fold (r, flags);
3450 : 2576516 : break;
3451 : : }
3452 : :
3453 : 108709502 : optimize = nw;
3454 : :
3455 : : /* Invoke maybe_constant_value for functions declared
3456 : : constexpr and not called with AGGR_INIT_EXPRs.
3457 : : TODO:
3458 : : Do constexpr expansion of expressions where the call itself is not
3459 : : constant, but the call followed by an INDIRECT_REF is. */
3460 : 107318401 : if (callee && DECL_DECLARED_CONSTEXPR_P (callee)
3461 : 145221302 : && (!flag_no_inline
3462 : 1201440 : || lookup_attribute ("always_inline",
3463 : 1201440 : DECL_ATTRIBUTES (callee))))
3464 : : {
3465 : 35554100 : mce_value manifestly_const_eval = mce_unknown;
3466 : 35554100 : if (flags & ff_mce_false)
3467 : : /* Allow folding __builtin_is_constant_evaluated to false during
3468 : : constexpr evaluation of this call. */
3469 : 26251540 : manifestly_const_eval = mce_false;
3470 : 35554100 : r = maybe_constant_value (x, /*decl=*/NULL_TREE,
3471 : : manifestly_const_eval);
3472 : : }
3473 : 108709502 : optimize = sv;
3474 : :
3475 : 108709502 : if (TREE_CODE (r) != CALL_EXPR)
3476 : : {
3477 : 6129082 : if (DECL_CONSTRUCTOR_P (callee))
3478 : 294 : r = cp_build_init_expr_for_ctor (x, r);
3479 : 3064541 : x = r;
3480 : 3064541 : break;
3481 : : }
3482 : :
3483 : : break;
3484 : : }
3485 : :
3486 : 20022916 : case CONSTRUCTOR:
3487 : 20022916 : {
3488 : 20022916 : unsigned i;
3489 : 20022916 : constructor_elt *p;
3490 : 20022916 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (x);
3491 : 20022916 : vec<constructor_elt, va_gc> *nelts = NULL;
3492 : 50281294 : FOR_EACH_VEC_SAFE_ELT (elts, i, p)
3493 : : {
3494 : 30258378 : tree op = cp_fold (p->value, flags);
3495 : 30258378 : if (op == error_mark_node)
3496 : : {
3497 : 0 : x = error_mark_node;
3498 : 0 : vec_free (nelts);
3499 : : break;
3500 : : }
3501 : 30258378 : else if (op != p->value)
3502 : : {
3503 : 1095310 : if (nelts == NULL)
3504 : 820570 : nelts = elts->copy ();
3505 : 1095310 : (*nelts)[i].value = op;
3506 : : }
3507 : : }
3508 : 20022916 : if (nelts)
3509 : : {
3510 : 820570 : x = build_constructor (TREE_TYPE (x), nelts);
3511 : 820570 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY (x)
3512 : 820570 : = CONSTRUCTOR_PLACEHOLDER_BOUNDARY (org_x);
3513 : 820570 : CONSTRUCTOR_MUTABLE_POISON (x)
3514 : 1641140 : = CONSTRUCTOR_MUTABLE_POISON (org_x);
3515 : : }
3516 : 20022916 : if (VECTOR_TYPE_P (TREE_TYPE (x)))
3517 : 43091 : x = fold (x);
3518 : : break;
3519 : : }
3520 : 487412 : case TREE_VEC:
3521 : 487412 : {
3522 : 487412 : bool changed = false;
3523 : 487412 : int n = TREE_VEC_LENGTH (x);
3524 : :
3525 : 1184812 : for (int i = 0; i < n; i++)
3526 : : {
3527 : 697400 : tree op = cp_fold (TREE_VEC_ELT (x, i), flags);
3528 : 697400 : if (op != TREE_VEC_ELT (x, i))
3529 : : {
3530 : 806 : if (!changed)
3531 : 773 : x = copy_node (x);
3532 : 806 : TREE_VEC_ELT (x, i) = op;
3533 : 806 : changed = true;
3534 : : }
3535 : : }
3536 : : }
3537 : :
3538 : : break;
3539 : :
3540 : 1705302 : case ARRAY_REF:
3541 : 1705302 : case ARRAY_RANGE_REF:
3542 : :
3543 : 1705302 : loc = EXPR_LOCATION (x);
3544 : 1705302 : op0 = cp_fold (TREE_OPERAND (x, 0), flags);
3545 : 1705302 : op1 = cp_fold (TREE_OPERAND (x, 1), flags);
3546 : 1705302 : op2 = cp_fold (TREE_OPERAND (x, 2), flags);
3547 : 1705302 : op3 = cp_fold (TREE_OPERAND (x, 3), flags);
3548 : :
3549 : 1705302 : if (op0 == error_mark_node
3550 : 1705302 : || op1 == error_mark_node
3551 : 1705302 : || op2 == error_mark_node
3552 : 1705302 : || op3 == error_mark_node)
3553 : 0 : x = error_mark_node;
3554 : 1705302 : else if (op0 != TREE_OPERAND (x, 0)
3555 : 747448 : || op1 != TREE_OPERAND (x, 1)
3556 : 366737 : || op2 != TREE_OPERAND (x, 2)
3557 : 2072039 : || op3 != TREE_OPERAND (x, 3))
3558 : : {
3559 : 1338565 : x = build4_loc (loc, code, TREE_TYPE (x), op0, op1, op2, op3);
3560 : 1338565 : TREE_READONLY (x) = TREE_READONLY (org_x);
3561 : 1338565 : TREE_SIDE_EFFECTS (x) = TREE_SIDE_EFFECTS (org_x);
3562 : 1338565 : TREE_THIS_VOLATILE (x) = TREE_THIS_VOLATILE (org_x);
3563 : : }
3564 : :
3565 : 1705302 : x = fold (x);
3566 : 1705302 : break;
3567 : :
3568 : 1175230 : case SAVE_EXPR:
3569 : : /* A SAVE_EXPR might contain e.g. (0 * i) + (0 * j), which, after
3570 : : folding, evaluates to an invariant. In that case no need to wrap
3571 : : this folded tree with a SAVE_EXPR. */
3572 : 1175230 : r = cp_fold (TREE_OPERAND (x, 0), flags);
3573 : 1175230 : if (tree_invariant_p (r))
3574 : 57 : x = r;
3575 : : break;
3576 : :
3577 : 8 : case REQUIRES_EXPR:
3578 : 8 : x = evaluate_requires_expr (x);
3579 : 8 : break;
3580 : :
3581 : : default:
3582 : : return org_x;
3583 : : }
3584 : :
3585 : 1222830763 : if (EXPR_P (x) && TREE_CODE (x) == code)
3586 : : {
3587 : 859489047 : TREE_THIS_VOLATILE (x) = TREE_THIS_VOLATILE (org_x);
3588 : 859489047 : copy_warning (x, org_x);
3589 : : }
3590 : :
3591 : 1222830763 : if (!c.evaluation_restricted_p ())
3592 : : {
3593 : 1222611476 : fold_cache->put (org_x, x);
3594 : : /* Prevent that we try to fold an already folded result again. */
3595 : 1222611476 : if (x != org_x)
3596 : 656607264 : fold_cache->put (x, x);
3597 : : }
3598 : :
3599 : : return x;
3600 : : }
3601 : :
3602 : : /* Look up "hot", "cold", "likely" or "unlikely" in attribute list LIST. */
3603 : :
3604 : : tree
3605 : 244889786 : lookup_hotness_attribute (tree list)
3606 : : {
3607 : 244919350 : for (; list; list = TREE_CHAIN (list))
3608 : : {
3609 : 666088 : tree name = get_attribute_name (list);
3610 : 666088 : if ((is_attribute_p ("hot", name)
3611 : 666088 : || is_attribute_p ("cold", name)
3612 : 666085 : || is_attribute_p ("likely", name)
3613 : 434703 : || is_attribute_p ("unlikely", name))
3614 : 1302621 : && is_attribute_namespace_p ("", list))
3615 : : break;
3616 : : }
3617 : 244889786 : return list;
3618 : : }
3619 : :
3620 : : /* Remove "hot", "cold", "likely" and "unlikely" attributes from LIST. */
3621 : :
3622 : : static tree
3623 : 636515 : remove_hotness_attribute (tree list)
3624 : : {
3625 : 1273048 : for (tree *p = &list; *p; )
3626 : : {
3627 : 636533 : tree l = *p;
3628 : 636533 : tree name = get_attribute_name (l);
3629 : 636533 : if ((is_attribute_p ("hot", name)
3630 : 636533 : || is_attribute_p ("cold", name)
3631 : 636530 : || is_attribute_p ("likely", name)
3632 : 405148 : || is_attribute_p ("unlikely", name))
3633 : 1273066 : && is_attribute_namespace_p ("", l))
3634 : : {
3635 : 636524 : *p = TREE_CHAIN (l);
3636 : 636524 : continue;
3637 : : }
3638 : 9 : p = &TREE_CHAIN (l);
3639 : : }
3640 : 636515 : return list;
3641 : : }
3642 : :
3643 : : /* If [[likely]] or [[unlikely]] appear on this statement, turn it into a
3644 : : PREDICT_EXPR. */
3645 : :
3646 : : tree
3647 : 244253289 : process_stmt_hotness_attribute (tree std_attrs, location_t attrs_loc)
3648 : : {
3649 : 244253289 : if (std_attrs == error_mark_node)
3650 : : return std_attrs;
3651 : 244253271 : if (tree attr = lookup_hotness_attribute (std_attrs))
3652 : : {
3653 : 636515 : tree name = get_attribute_name (attr);
3654 : 636515 : bool hot = (is_attribute_p ("hot", name)
3655 : 636515 : || is_attribute_p ("likely", name));
3656 : 636515 : tree pred = build_predict_expr (hot ? PRED_HOT_LABEL : PRED_COLD_LABEL,
3657 : : hot ? TAKEN : NOT_TAKEN);
3658 : 636515 : SET_EXPR_LOCATION (pred, attrs_loc);
3659 : 636515 : add_stmt (pred);
3660 : 636515 : if (tree other = lookup_hotness_attribute (TREE_CHAIN (attr)))
3661 : : {
3662 : 9 : auto_urlify_attributes sentinel;
3663 : 9 : warning (OPT_Wattributes, "ignoring attribute %qE after earlier %qE",
3664 : : get_attribute_name (other), name);
3665 : 9 : }
3666 : 636515 : std_attrs = remove_hotness_attribute (std_attrs);
3667 : : }
3668 : : return std_attrs;
3669 : : }
3670 : :
3671 : : /* Build IFN_ASSUME internal call for assume condition ARG. */
3672 : :
3673 : : tree
3674 : 11064 : build_assume_call (location_t loc, tree arg)
3675 : : {
3676 : 11064 : if (!processing_template_decl)
3677 : 11019 : arg = fold_build_cleanup_point_expr (TREE_TYPE (arg), arg);
3678 : 11064 : return build_call_expr_internal_loc (loc, IFN_ASSUME, void_type_node,
3679 : 11064 : 1, arg);
3680 : : }
3681 : :
3682 : : /* If [[assume (cond)]] appears on this statement, handle it. */
3683 : :
3684 : : tree
3685 : 202368054 : process_stmt_assume_attribute (tree std_attrs, tree statement,
3686 : : location_t attrs_loc)
3687 : : {
3688 : 202368054 : if (std_attrs == error_mark_node)
3689 : : return std_attrs;
3690 : 202368036 : tree attr = lookup_attribute ("gnu", "assume", std_attrs);
3691 : 202368036 : if (!attr)
3692 : : return std_attrs;
3693 : : /* The next token after the assume attribute is not ';'. */
3694 : 10987 : if (statement)
3695 : : {
3696 : 12 : warning_at (attrs_loc, OPT_Wattributes,
3697 : : "%<assume%> attribute not followed by %<;%>");
3698 : 12 : attr = NULL_TREE;
3699 : : }
3700 : 21998 : for (; attr; attr = lookup_attribute ("gnu", "assume", TREE_CHAIN (attr)))
3701 : : {
3702 : 11011 : tree args = TREE_VALUE (attr);
3703 : 11011 : if (args && PACK_EXPANSION_P (args))
3704 : : {
3705 : 6 : auto_diagnostic_group d;
3706 : 6 : error_at (attrs_loc, "pack expansion of %qE attribute",
3707 : : get_attribute_name (attr));
3708 : 6 : if (cxx_dialect >= cxx17)
3709 : 4 : inform (attrs_loc, "use fold expression in the attribute "
3710 : : "argument instead");
3711 : 6 : continue;
3712 : 6 : }
3713 : 11005 : int nargs = list_length (args);
3714 : 11005 : if (nargs != 1)
3715 : : {
3716 : 42 : auto_diagnostic_group d;
3717 : 42 : error_at (attrs_loc, "wrong number of arguments specified for "
3718 : : "%qE attribute", get_attribute_name (attr));
3719 : 42 : inform (attrs_loc, "expected %i, found %i", 1, nargs);
3720 : 42 : }
3721 : : else
3722 : : {
3723 : 10963 : tree arg = TREE_VALUE (args);
3724 : 10963 : if (!type_dependent_expression_p (arg))
3725 : 10918 : arg = contextual_conv_bool (arg, tf_warning_or_error);
3726 : 10963 : if (error_operand_p (arg))
3727 : 18 : continue;
3728 : 10945 : finish_expr_stmt (build_assume_call (attrs_loc, arg));
3729 : : }
3730 : : }
3731 : 10987 : return remove_attribute ("gnu", "assume", std_attrs);
3732 : : }
3733 : :
3734 : : /* Return the type std::source_location::__impl after performing
3735 : : verification on it. */
3736 : :
3737 : : tree
3738 : 2363 : get_source_location_impl_type ()
3739 : : {
3740 : 2363 : tree name = get_identifier ("source_location");
3741 : 2363 : tree decl = lookup_qualified_name (std_node, name);
3742 : 2363 : if (TREE_CODE (decl) != TYPE_DECL)
3743 : : {
3744 : 6 : auto_diagnostic_group d;
3745 : 6 : if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
3746 : 3 : qualified_name_lookup_error (std_node, name, decl, input_location);
3747 : : else
3748 : 3 : error ("%qD is not a type", decl);
3749 : 6 : return error_mark_node;
3750 : 6 : }
3751 : 2357 : name = get_identifier ("__impl");
3752 : 2357 : tree type = TREE_TYPE (decl);
3753 : 2357 : decl = lookup_qualified_name (type, name);
3754 : 2357 : if (TREE_CODE (decl) != TYPE_DECL)
3755 : : {
3756 : 9 : auto_diagnostic_group d;
3757 : 9 : if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
3758 : 6 : qualified_name_lookup_error (type, name, decl, input_location);
3759 : : else
3760 : 3 : error ("%qD is not a type", decl);
3761 : 9 : return error_mark_node;
3762 : 9 : }
3763 : 2348 : type = TREE_TYPE (decl);
3764 : 2348 : if (TREE_CODE (type) != RECORD_TYPE)
3765 : : {
3766 : 3 : error ("%qD is not a class type", decl);
3767 : 3 : return error_mark_node;
3768 : : }
3769 : :
3770 : 2345 : int cnt = 0;
3771 : 2345 : for (tree field = TYPE_FIELDS (type);
3772 : 11686 : (field = next_aggregate_field (field)) != NULL_TREE;
3773 : 9341 : field = DECL_CHAIN (field))
3774 : : {
3775 : 9350 : if (DECL_NAME (field) != NULL_TREE)
3776 : : {
3777 : 9350 : const char *n = IDENTIFIER_POINTER (DECL_NAME (field));
3778 : 9350 : if (strcmp (n, "_M_file_name") == 0
3779 : 7008 : || strcmp (n, "_M_function_name") == 0)
3780 : : {
3781 : 4681 : if (TREE_TYPE (field) != const_string_type_node)
3782 : : {
3783 : 3 : error ("%qD does not have %<const char *%> type", field);
3784 : 3 : return error_mark_node;
3785 : : }
3786 : 4678 : cnt++;
3787 : 4678 : continue;
3788 : : }
3789 : 4669 : else if (strcmp (n, "_M_line") == 0 || strcmp (n, "_M_column") == 0)
3790 : : {
3791 : 4666 : if (TREE_CODE (TREE_TYPE (field)) != INTEGER_TYPE)
3792 : : {
3793 : 3 : error ("%qD does not have integral type", field);
3794 : 3 : return error_mark_node;
3795 : : }
3796 : 4663 : cnt++;
3797 : 4663 : continue;
3798 : : }
3799 : : }
3800 : : cnt = 0;
3801 : : break;
3802 : : }
3803 : 2336 : if (cnt != 4)
3804 : : {
3805 : 9 : error ("%<std::source_location::__impl%> does not contain only "
3806 : : "non-static data members %<_M_file_name%>, "
3807 : : "%<_M_function_name%>, %<_M_line%> and %<_M_column%>");
3808 : 9 : return error_mark_node;
3809 : : }
3810 : 2330 : return build_qualified_type (type, TYPE_QUAL_CONST);
3811 : : }
3812 : :
3813 : : /* Type for source_location_table hash_set. */
3814 : : struct GTY((for_user)) source_location_table_entry {
3815 : : location_t loc;
3816 : : unsigned uid;
3817 : : tree var;
3818 : : };
3819 : :
3820 : : /* Traits class for function start hash maps below. */
3821 : :
3822 : : struct source_location_table_entry_hash
3823 : : : ggc_remove <source_location_table_entry>
3824 : : {
3825 : : typedef source_location_table_entry value_type;
3826 : : typedef source_location_table_entry compare_type;
3827 : :
3828 : : static hashval_t
3829 : 611 : hash (const source_location_table_entry &ref)
3830 : : {
3831 : 611 : inchash::hash hstate (0);
3832 : 611 : hstate.add_int (ref.loc);
3833 : 611 : hstate.add_int (ref.uid);
3834 : 611 : return hstate.end ();
3835 : : }
3836 : :
3837 : : static bool
3838 : 387 : equal (const source_location_table_entry &ref1,
3839 : : const source_location_table_entry &ref2)
3840 : : {
3841 : 387 : return ref1.loc == ref2.loc && ref1.uid == ref2.uid;
3842 : : }
3843 : :
3844 : : static void
3845 : : mark_deleted (source_location_table_entry &ref)
3846 : : {
3847 : : ref.loc = UNKNOWN_LOCATION;
3848 : : ref.uid = -1U;
3849 : : ref.var = NULL_TREE;
3850 : : }
3851 : :
3852 : : static const bool empty_zero_p = true;
3853 : :
3854 : : static void
3855 : 0 : mark_empty (source_location_table_entry &ref)
3856 : : {
3857 : 0 : ref.loc = UNKNOWN_LOCATION;
3858 : 0 : ref.uid = 0;
3859 : 0 : ref.var = NULL_TREE;
3860 : : }
3861 : :
3862 : : static bool
3863 : 402 : is_deleted (const source_location_table_entry &ref)
3864 : : {
3865 : 402 : return (ref.loc == UNKNOWN_LOCATION
3866 : 0 : && ref.uid == -1U
3867 : 402 : && ref.var == NULL_TREE);
3868 : : }
3869 : :
3870 : : static bool
3871 : 6404 : is_empty (const source_location_table_entry &ref)
3872 : : {
3873 : 6404 : return (ref.loc == UNKNOWN_LOCATION
3874 : 5393 : && ref.uid == 0
3875 : 11797 : && ref.var == NULL_TREE);
3876 : : }
3877 : :
3878 : : static void
3879 : 3 : pch_nx (source_location_table_entry &p)
3880 : : {
3881 : 3 : extern void gt_pch_nx (source_location_table_entry &);
3882 : 3 : gt_pch_nx (p);
3883 : 3 : }
3884 : :
3885 : : static void
3886 : 3 : pch_nx (source_location_table_entry &p, gt_pointer_operator op, void *cookie)
3887 : : {
3888 : 3 : extern void gt_pch_nx (source_location_table_entry *, gt_pointer_operator,
3889 : : void *);
3890 : 3 : gt_pch_nx (&p, op, cookie);
3891 : 3 : }
3892 : : };
3893 : :
3894 : : static GTY(()) hash_table <source_location_table_entry_hash>
3895 : : *source_location_table;
3896 : :
3897 : : /* Fold the __builtin_source_location () call T. */
3898 : :
3899 : : tree
3900 : 436 : fold_builtin_source_location (const_tree t)
3901 : : {
3902 : 436 : gcc_assert (TREE_CODE (t) == CALL_EXPR);
3903 : : /* TREE_TYPE (t) is const std::source_location::__impl* */
3904 : 436 : tree source_location_impl = TREE_TYPE (TREE_TYPE (t));
3905 : 436 : if (source_location_impl == error_mark_node)
3906 : 0 : return build_zero_cst (const_ptr_type_node);
3907 : 436 : gcc_assert (CLASS_TYPE_P (source_location_impl)
3908 : : && id_equal (TYPE_IDENTIFIER (source_location_impl), "__impl"));
3909 : :
3910 : 436 : location_t loc = EXPR_LOCATION (t);
3911 : 436 : if (source_location_table == NULL)
3912 : 45 : source_location_table
3913 : 45 : = hash_table <source_location_table_entry_hash>::create_ggc (64);
3914 : 436 : const line_map_ordinary *map;
3915 : 436 : source_location_table_entry entry;
3916 : 436 : entry.loc
3917 : 436 : = linemap_resolve_location (line_table, loc, LRK_MACRO_EXPANSION_POINT,
3918 : : &map);
3919 : 436 : entry.uid = current_function_decl ? DECL_UID (current_function_decl) : -1;
3920 : 436 : entry.var = error_mark_node;
3921 : 436 : source_location_table_entry *entryp
3922 : 436 : = source_location_table->find_slot (entry, INSERT);
3923 : 436 : tree var;
3924 : 436 : if (entryp->var)
3925 : : var = entryp->var;
3926 : : else
3927 : : {
3928 : 226 : var = build_decl (loc, VAR_DECL, generate_internal_label ("Lsrc_loc"),
3929 : : source_location_impl);
3930 : 226 : TREE_STATIC (var) = 1;
3931 : 226 : TREE_PUBLIC (var) = 0;
3932 : 226 : DECL_ARTIFICIAL (var) = 1;
3933 : 226 : DECL_IGNORED_P (var) = 1;
3934 : 226 : DECL_EXTERNAL (var) = 0;
3935 : 226 : DECL_DECLARED_CONSTEXPR_P (var) = 1;
3936 : 226 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var) = 1;
3937 : 226 : layout_decl (var, 0);
3938 : :
3939 : 226 : vec<constructor_elt, va_gc> *v = NULL;
3940 : 226 : vec_alloc (v, 4);
3941 : 226 : for (tree field = TYPE_FIELDS (source_location_impl);
3942 : 1130 : (field = next_aggregate_field (field)) != NULL_TREE;
3943 : 904 : field = DECL_CHAIN (field))
3944 : : {
3945 : 904 : const char *n = IDENTIFIER_POINTER (DECL_NAME (field));
3946 : 904 : tree val = NULL_TREE;
3947 : 904 : if (strcmp (n, "_M_file_name") == 0)
3948 : : {
3949 : 226 : if (const char *fname = LOCATION_FILE (loc))
3950 : : {
3951 : 226 : fname = remap_macro_filename (fname);
3952 : 226 : val = build_string_literal (fname);
3953 : : }
3954 : : else
3955 : 0 : val = build_string_literal ("");
3956 : : }
3957 : 678 : else if (strcmp (n, "_M_function_name") == 0)
3958 : : {
3959 : 226 : const char *name = "";
3960 : :
3961 : 226 : if (current_function_decl)
3962 : : {
3963 : : /* If this is a coroutine, we should get the name of the user
3964 : : function rather than the actor we generate. */
3965 : 181 : if (tree ramp = DECL_RAMP_FN (current_function_decl))
3966 : 12 : name = cxx_printable_name (ramp, 2);
3967 : : else
3968 : 169 : name = cxx_printable_name (current_function_decl, 2);
3969 : : }
3970 : :
3971 : 226 : val = build_string_literal (name);
3972 : : }
3973 : 452 : else if (strcmp (n, "_M_line") == 0)
3974 : 226 : val = build_int_cst (TREE_TYPE (field), LOCATION_LINE (loc));
3975 : 226 : else if (strcmp (n, "_M_column") == 0)
3976 : 226 : val = build_int_cst (TREE_TYPE (field), LOCATION_COLUMN (loc));
3977 : : else
3978 : 0 : gcc_unreachable ();
3979 : 904 : CONSTRUCTOR_APPEND_ELT (v, field, val);
3980 : : }
3981 : :
3982 : 226 : tree ctor = build_constructor (source_location_impl, v);
3983 : 226 : TREE_CONSTANT (ctor) = 1;
3984 : 226 : TREE_STATIC (ctor) = 1;
3985 : 226 : DECL_INITIAL (var) = ctor;
3986 : 226 : varpool_node::finalize_decl (var);
3987 : 226 : *entryp = entry;
3988 : 226 : entryp->var = var;
3989 : : }
3990 : :
3991 : 436 : return build_fold_addr_expr_with_type_loc (loc, var, TREE_TYPE (t));
3992 : : }
3993 : :
3994 : : #include "gt-cp-cp-gimplify.h"
|